Skip to content
CodeKruPro

Explicit Wait - Element Visible

36/70

Explicit Wait - visibilityOfElementLocated

An explicit wait waits for a specific condition before proceeding. Use WebDriverWait with ExpectedConditions.

On the Appearing Elements page, the element with ID appearing5sec becomes visible after 5 seconds.

Task:

  1. Navigate to https://testkru.com/Interactions/AppearingElements
  2. Use WebDriverWait to wait for appearing5sec to become visible (max 10 seconds)
  3. Print its text once visible

Expected output:

Element visible: Message appeared after 5 seconds

About this lesson

WebDriverWait with visibilityOfElementLocated in Selenium Java

Learn Selenium JavaIntermediateWaits

How it works

An explicit wait waits for one condition, for one element, at one point in the test. You create new WebDriverWait(driver, Duration.ofSeconds(10)) and pass a condition to until(). The wait checks the condition every 500 milliseconds and returns as soon as it holds. If the time runs out, it throws TimeoutException.

ExpectedConditions.visibilityOfElementLocated() holds when an element matching the locator is in the DOM and displayed, and until() then returns that WebElement, ready to use. That return value is why you rarely need a separate findElement() after a wait.

The Appearing Elements page shows why visibility matters. The paragraph appearing5sec is in the HTML from the start but hidden by the page's CSS (display: none), so findElement() finds it at once and getText() returns an empty string. presenceOfElementLocated() would also return immediately. Only a visibility wait holds until the text can actually be seen.

About the assignment

A message on Appearing Elements becomes visible about five seconds after the page loads. You wait for it with a ten-second explicit wait and print its text, so the read happens only once the message is visible.

What you will do

  1. Create a WebDriverWait for the driver with a ten-second Duration.
  2. Call until() with ExpectedConditions.visibilityOfElementLocated() and the id from the task, and keep the element it returns.
  3. Print that element's text after the label Element visible:.

Common mistakes

  • Finding the element before waiting

    findElement() succeeds because the element is already in the DOM, but it is hidden, so getText() returns an empty string and the line prints with nothing after the label.

  • Using Thread.sleep()

    A fixed sleep either wastes time or is too short on a slow run. The wait returns as soon as the message shows.

  • A timeout shorter than the delay

    A two-second wait on a five-second element throws TimeoutException with "Expected condition failed: waiting for visibility of element found by By.id". Size the timeout for the slowest normal case.

In a real test suite

Explicit visibility waits are the backbone of stable UI tests: wait for a toast after saving, for search results after typing, for a modal after clicking. Most teams create one WebDriverWait per test or page object and reuse it.

Lesson 35 waited for presence across the whole session. Lesson 37 waits for a button to be clickable, which adds the enabled check.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Interactions/AppearingElements");
        
        // TODO: Create WebDriverWait with 10 second timeout
        // TODO: Wait for appearing5sec to be visible
        // TODO: Print its text
        
        driver.quit();
    }
}