About this lesson
Handle dynamic elements in Selenium Java: wait for one, check the state of others
Learn Selenium JavaAdvancedAdvanced Topics
How it works
Dynamic content changes after the page loads: messages that appear on a timer, results that arrive from a request, sections that hide again. The rule is to wait for the specific thing you need, then check other elements as they are at that moment, not as they will be later.
On Appearing Elements all three messages are in the DOM from the start and hidden. When the five-second message becomes visible, the ten-second and fifteen-second messages are still hidden, so at that moment isDisplayed() on them returns false and getText() returns an empty string.
A wait can also hold until something is not true. ExpectedConditions.not() wraps another condition, so wrapping a visibility condition waits until an element is not visible. Checking "still hidden" needs no wait at all, only an immediate isDisplayed().
About the assignment
The starter creates a ten-second wait. You wait until the first message is visible, print it, and at that same moment check that the second message has not appeared yet.
What you will do
- Wait for the five-second message with
visibilityOfElementLocated()and keep the returned element. - Print its text after the label
First element:. - Find the ten-second message and print whether it is still hidden, the negation of
isDisplayed(), after the labelSecond still hidden:.
Common mistakes
Waiting for the second message too
If you wait for it before checking, it is visible by then and the check prints the wrong value.
Checking presence instead of visibility
The second message is already in the DOM, so a presence check says it is there. Visibility is the state that changes.
Printing isDisplayed() without negating it
The label asks whether the element is still hidden, which is the opposite of displayed.
In a real test suite
Checking state at a moment is common in tests for progressive loading, for example "the summary has loaded but the detailed chart is still pending", and for making sure an error message did not appear alongside a success message.
Lesson 68 handled cookies. The last lesson, 70, reads the whole page source.
Related lessons
- Explicit Wait - Element VisibleThe single visibility wait this lesson builds on.
- Wait for Elements Appearing at Different TimesWait for each message in turn on the same page.
- isDisplayed() - Check Element Visibility
isDisplayed()for the "still hidden" 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.
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(10));
// TODO: Wait for appearing5sec to become visible
// TODO: Print its text
// TODO: Check if appearing10sec is still not visible
driver.quit();
}
}