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
- Load the page and find the 5-second message once, keeping the element in a variable.
- Read its displayed state with
is_displayed()and print it after the first label. - Build a
WebDriverWaitwith a timeout above 5 seconds and calluntilwith the invisibility condition for that same message. - Check the state again, flip it with
notso a hidden element reads as success, and print it after the second label, rememberingstr().
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 bareMessage:line. Allow more time than the page needs.Joining a boolean to a string
Writing a label plus the result of
is_displayed()fails withTypeErrorwith the messagecan only concatenate str (not "bool") to str. Wrap the value instr().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.
Related lessons
- Wait Until an Element Is VisibleThe mirror image: wait until an element becomes visible instead of hidden.
- Check Whether an Element Is DisplayedCovers
is_displayed(), the check this exercise runs before and after the wait. - Diagnose a Wait That Times OutShows what a wait looks like when it gives up and raises
TimeoutException.
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.
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()