Skip to content
CodeKruPro

Wait for Two Elements That Appear at Different Times

42/80

Wait for Two Elements That Appear at Different Times

Two waits can run one after the other. The second element appears later, so the second wait must be long enough for it.

Task:

  1. Wait for #appearing5sec and print its text
  2. Wait for #appearing10sec and print its text

Do not call implicitly_wait.

Print the lines below exactly.

Expected output:

First: Message appeared after 5 seconds
Second: Message appeared after 10 seconds

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

  1. Open the page and create a wait for the 5-second message, using a timeout of at least 10 seconds.
  2. Store the element returned by the wait and print its text after the first label.
  3. 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.
  4. Print the second text after its label. Do not call implicitly_wait anywhere.

Common mistakes

  • Reading text without waiting

    A direct find works, because the element is in the page, but text comes 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 TimeoutException with 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 TypeError with the message visibility_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.

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

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