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
- Wait for the first message with
visibilityOfElementLocated()and print its text after the labelFirst:. - 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
WebDriverWaitcan run any number ofuntil()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.
Related lessons
- Explicit Wait - Element VisibleA single visibility wait, explained step by step.
- Handle Dynamic Elements (Appearing Content)The same page, approached as a general problem of dynamic content.
- Implicit WaitThe implicit wait, and why not to mix it with these.
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.
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();
}
}