Skip to content
CodeKruPro

Wait Until an Element Is Visible

39/80

Wait Until an Element Is Visible

WebDriverWait plus visibility_of_element_located polls until the element is shown or the timeout ends. Do not use time.sleep. Do not call implicitly_wait.

Task:

  1. Open Appearing Elements
  2. Wait up to 10 seconds for #appearing5sec to be visible
  3. Print its text

Print the lines below exactly.

Expected output:

Element visible: Message appeared after 5 seconds

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

  1. The starter already imports WebDriverWait and expected_conditions as EC. Use them with a ten second limit.
  2. Give EC.visibility_of_element_located a tuple that names the message by its id.
  3. Keep the element that until returns and take its text.
  4. Print the label from the task and then the text.

Common mistakes

  • Passing the locator without a tuple

    Two separate arguments raise a TypeError about positional arguments. A bare string also fails with a TypeError from find_element. Use one tuple.

  • Printing the element

    The wait gives you a WebElement, not text. Adding it to a string raises TypeError. Add .text first.

  • Choosing a timeout that is too short

    The message appears after about five seconds. A two second wait ends in TimeoutException with 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.

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 until #appearing5sec is visible and print its text
finally:
    driver.quit()