About this lesson
Selenium Python TimeoutException: catch a wait that is too short, then read the element
Selenium Python AssignmentsIntermediateWaits
How it works
When WebDriverWait.until runs out of time, it raises TimeoutException. The class lives in selenium.common.exceptions, and the starter already imports it. In Python the failure is a traceback ending with the exception name and a Message: line, which is empty for a plain wait. The exception type is the useful part.
Catching it is ordinary Python: a try block around the wait and an except TimeoutException clause under it. A timeout is information, not always a bug. The 15-second message is hidden but present from the start, so the short wait fails on visibility, not on a missing element, and a longer wait on the same locator can still succeed.
The last step uses assert, a statement and not a function. It raises AssertionError when its condition is false. To test that a phrase appears in a longer text, use the in operator on strings.
About the assignment
Show both outcomes of a wait on the same element: a short wait that fails and is caught, followed by a longer wait that succeeds.
What you will do
- Open the Appearing Elements page. Wrap a 2 second wait for the 15-second message in
try, and catchTimeoutException. - Inside the
exceptblock, print the failure line, using the exception name as the label expects. - After the block, make a second wait that is long enough for the full 15 seconds and store the element it returns.
- Print its text after the second label, then
assertthat the text mentions 15 seconds.
Common mistakes
Catching the wrong class
A bare
excepthides other problems, andNoSuchElementExceptionnever fires here because the element exists. NameTimeoutException.A second wait that is still short
The first wait used up 2 seconds, but the page needs about 15 in total. A second wait of 10 seconds can still fail with
TimeoutException. Give it clear margin.Forgetting the import
Using the name without importing it gives
NameError: name 'TimeoutException' is not definedat the line where theexceptclause is evaluated, which is only reached after the wait fails.
In a real test suite
In a real suite, a failing wait is a test failure, and the TimeoutException traceback tells you what never appeared. Teams often wrap until in a helper that adds a custom message naming the awaited element.
The next group of lessons leaves waits and moves to alerts, which are not elements at all.
Related lessons
- Wait for Two Elements That Appear at Different TimesWaits for two elements on the same page without any failure.
- Wait Until an Element Is VisibleThe basic visibility wait whose timeout you shorten here.
- Accept a Simple AlertStarts the alerts group, where a different exception, for a missing alert, shows up.
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
from selenium.common.exceptions import TimeoutException
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Interactions/AppearingElements")
# A short wait should fail. A longer wait should read the 15 second message.
finally:
driver.quit()