Skip to content
CodeKruPro

Wait Until an Element Disappears

41/80

Wait Until an Element Disappears

invisibility_of_element_located waits until the element is gone or hidden. Use the 5-second element only. Do not use the 10-second or 15-second elements here.

Task:

  1. Open Disappearing Elements
  2. Print whether #disappearing5sec is displayed
  3. Wait until it is invisible, then print whether it is still displayed

Print the result of is_displayed() after the wait. This Selenium version can return the element from the wait, so do not print that return value.

Print the lines below exactly.

Expected output:

Initially visible: True
Disappeared: True

About this lesson

Selenium Python invisibility_of_element_located: pause a script until a message goes away

Selenium Python AssignmentsIntermediateWaits

How it works

Most waits ask whether something has arrived. invisibility_of_element_located asks the opposite: has it left? You hand it one locator tuple, and WebDriverWait(...).until() keeps polling until the element is hidden or gone. Two separate arguments instead of one tuple raise a TypeError.

On the practice page the 5-second message is not deleted. It stays in the document and is only hidden, so an element you grabbed earlier is still usable. is_displayed() gives True before the wait and False after it, and its text becomes an empty string once hidden.

This condition has a trap. If the locator matches nothing at all, the wait counts that as invisible and returns at once, so a typo passes instantly and your later check runs too early. The return value differs too: a missing locator gives True, while an element that was present and then hid comes back as a WebElement, so do not print the result as a pass or fail flag. A boolean cannot be joined to text with +, so convert it with str() first.

About the assignment

Open the Disappearing Elements page, record that the 5-second message starts visible, wait for it to vanish, then record that it is gone. Ignore the other messages.

What you will do

  1. Load the page and find the 5-second message once, keeping the element in a variable.
  2. Read its displayed state with is_displayed() and print it after the first label.
  3. Build a WebDriverWait with a timeout above 5 seconds and call until with the invisibility condition for that same message.
  4. Check the state again, flip it with not so a hidden element reads as success, and print it after the second label, remembering str().

Common mistakes

  • A timeout shorter than the page

    The message hides after about 5 seconds. A 2 second wait raises TimeoutException, and the traceback ends in a bare Message: line. Allow more time than the page needs.

  • Joining a boolean to a string

    Writing a label plus the result of is_displayed() fails with TypeError with the message can only concatenate str (not "bool") to str. Wrap the value in str().

  • Expecting the wait to return True

    Older tutorials assume a boolean. Here the wait gives back a WebElement, so printing it shows an object description, not the state you wanted.

In a real test suite

Teams use this wait for loading spinners, toast messages and overlays that block a click. A helper in a pytest suite takes a locator and a timeout, and runs right after the action that starts the spinner.

The next lesson goes back to elements that appear, and chains two waits where the second element shows up later than the first.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Interactions/DisappearingElements")
    # Print the starting state, then wait until #disappearing5sec is invisible
finally:
    driver.quit()