Skip to content
CodeKruPro

Wait for Elements Appearing at Different Times

39/70

Wait for Element to Appear

Combine multiple waits to handle elements that appear at different times.

On the Appearing Elements page:

  • appearing5sec appears after 5 seconds
  • appearing10sec appears after 10 seconds

Task:

  1. Navigate to https://testkru.com/Interactions/AppearingElements
  2. Wait for appearing5sec to become visible, print its text
  3. Wait for appearing10sec to become visible, print its text

Expected output:

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

About this lesson

Wait for multiple elements that appear at different times in Selenium Java

Learn Selenium JavaIntermediateWaits

How it works

One WebDriverWait can be reused for any number of conditions. Each until() call starts its own countdown from the full timeout, so a fifteen-second wait allows up to fifteen seconds for every element you pass it.

Waits run one after another, but the page's timers do not. On Appearing Elements both messages start their countdown when the page loads. The first wait returns after about five seconds, and the second then needs only about five more, so the whole task takes about ten seconds rather than fifteen.

There is a third message on the page, appearing15sec. Sizing the timeout means knowing which elements you depend on: waiting for that one too would need a total of about fifteen seconds.

About the assignment

The starter creates a fifteen-second wait. You use it twice, first for the five-second message and then for the ten-second one, printing each message once it is visible.

What you will do

  1. Wait for the first message with visibilityOfElementLocated() and print its text after the label First:.
  2. Use the same wait object for the second message and print its text after the label Second:.

Common mistakes

  • Creating a new wait for each element

    It works, but there is nothing to gain. One WebDriverWait can run any number of until() calls.

  • Expecting the waits to add up

    The two timers run in parallel from page load, so the second wait needs only about five more seconds and the whole run takes about ten, not fifteen. Size each timeout for the slowest single element.

  • Reading the second message before waiting

    It is in the DOM but hidden, so getText() returns an empty string. Each element needs its own wait.

In a real test suite

Dashboards that load widgets one by one and pages that stream in data in stages need several waits in sequence. Page objects often wrap each wait in a method such as waitForChart() so tests read as steps.

Lesson 38 waited for a disappearance. Lesson 40 leaves waits behind and handles browser alert dialogs.

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");
        
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
        
        // TODO: Wait for appearing5sec and print its text
        // TODO: Wait for appearing10sec and print its text
        
        driver.quit();
    }
}