Skip to content
CodeKruPro

Diagnose a Wait That Times Out

43/80

Diagnose a Wait That Times Out

A wait that is shorter than the element raises TimeoutException. Catch that exception, print its name, then wait long enough to read the same element.

Task:

  1. Use a 2 second wait on #appearing15sec
  2. Print Wait failed: TimeoutException
  3. Wait long enough to print the message
  4. Assert that the message mentions 15 seconds

Print the lines below exactly.

Expected output:

Wait failed: TimeoutException
Later: Message appeared after 15 seconds

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

  1. Open the Appearing Elements page. Wrap a 2 second wait for the 15-second message in try, and catch TimeoutException.
  2. Inside the except block, print the failure line, using the exception name as the label expects.
  3. After the block, make a second wait that is long enough for the full 15 seconds and store the element it returns.
  4. Print its text after the second label, then assert that the text mentions 15 seconds.

Common mistakes

  • Catching the wrong class

    A bare except hides other problems, and NoSuchElementException never fires here because the element exists. Name TimeoutException.

  • 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 defined at the line where the except clause 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.

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
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()