About this lesson
Selenium Python WebDriverWait with visibility_of_element_located: wait for a hidden message to appear
Selenium Python AssignmentsIntermediateWaits
How it works
An explicit wait waits for one condition, not for the whole session. You build WebDriverWait(driver, 10) and call until with a condition. It checks every half second and stops when the condition is true or ten seconds have passed.
EC.visibility_of_element_located finds the element and checks that it is displayed. When it succeeds, until returns the WebElement, so you can read text straight away. Adding the result to a string without .text raises TypeError with the message can only concatenate str (not "WebElement") to str.
Python needs a tuple for the locator. The condition takes one argument, so you pass one pair in brackets: a By constant and a string, inside an extra set of parentheses. Two separate arguments raise TypeError: visibility_of_element_located() takes 1 positional argument but 2 were given.
On this page the paragraph is in the DOM from the start but has display: none. While hidden its text is empty, because Selenium returns only visible text. Waiting for presence alone gives you the element and an empty string. Waiting for visibility gives the message.
When time runs out you get TimeoutException. Its message is empty, so printing it shows "Message:" and perhaps a stack trace.
About the assignment
Open the Appearing Elements page, wait for the five-second message to show up, and print its text. Use an explicit wait, not a sleep and not an implicit wait.
What you will do
- The starter already imports
WebDriverWaitandexpected_conditions as EC. Use them with a ten second limit. - Give
EC.visibility_of_element_locateda tuple that names the message by its id. - Keep the element that
untilreturns and take itstext. - Print the label from the task and then the text.
Common mistakes
Passing the locator without a tuple
Two separate arguments raise a
TypeErrorabout positional arguments. A bare string also fails with aTypeErrorfromfind_element. Use one tuple.Printing the element
The wait gives you a
WebElement, not text. Adding it to a string raisesTypeError. Add.textfirst.Choosing a timeout that is too short
The message appears after about five seconds. A two second wait ends in
TimeoutExceptionwith an empty message.
In a real test suite
Suites wrap this in a helper such as wait_visible(driver, locator) in a base page class, so no test repeats the timeout number.
The next lesson waits for a button to be clickable, which adds an enabled check to the visible check.
Related lessons
- Set an Implicit WaitCompare with the implicit wait, a global setting rather than one condition.
- Wait Until a Button Is ClickableWait for a button to be clickable, a stricter condition than visible.
- Wait Until an Element DisappearsWait the other way, until an element goes away.
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/AppearingElements")
# Wait until #appearing5sec is visible and print its text
finally:
driver.quit()