About this lesson
Selenium Python wait for two elements: chain two explicit waits for slow messages
Selenium Python AssignmentsIntermediateWaits
How it works
A WebDriverWait counts its timeout from the moment until is called, not from page load. The 5-second message is ready about 5 seconds after load, so when the first wait returns, the second element needs only about 5 more seconds.
All the messages exist in the document from the start. They are only hidden. find_element locates them with no error, but is_displayed() is False and text is an empty string, which is why a plain find followed by a print shows nothing. visibility_of_element_located waits until the element is shown and returns it, so you read text from the return value. The locator goes in as one tuple.
This lesson also sets a rule: no implicit wait. Mixing implicit and explicit waits makes timing hard to predict. Use two separate explicit waits, one after the other, each assigned to its own variable.
About the assignment
Read two messages from the Appearing Elements page: one that shows after about 5 seconds and one that shows after about 10. Print each as soon as its wait finishes.
What you will do
- Open the page and create a wait for the 5-second message, using a timeout of at least 10 seconds.
- Store the element returned by the wait and print its
textafter the first label. - Create a second wait for the 10-second message. Give it a limit large enough to cover the full 10 seconds, with room to spare.
- Print the second text after its label. Do not call
implicitly_waitanywhere.
Common mistakes
Reading text without waiting
A direct find works, because the element is in the page, but
textcomes back as an empty string. The line prints the label and nothing else, and no exception tells you why.A limit that is too tight
A wait of exactly the appearance time can lose the race. If it expires first, you get
TimeoutExceptionwith an empty message. Pick a limit clearly above the delay.Passing two arguments to the condition
Passing the strategy and the value as separate arguments gives
TypeErrorwith the messagevisibility_of_element_located() takes 1 positional argument but 2 were given. Wrap them as one tuple.
In a real test suite
In a pytest suite you rarely repeat the construction of WebDriverWait. A small helper such as wait_visible(driver, locator, timeout=10) lives in a base page class, and every page object calls it. The timeout becomes one default instead of a number scattered through tests.
The next lesson makes a wait fail on purpose, so you can read the failure.
Related lessons
- Wait Until an Element Is VisibleIntroduces
visibility_of_element_locatedwith a single wait. - Set an Implicit WaitExplains the implicit wait that this exercise tells you to leave out.
- Diagnose a Wait That Times OutTakes the same page and makes a wait fail on purpose.
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 for the 5 second message, then the 10 second message
finally:
driver.quit()