Skip to content
CodeKruPro

Handle Dynamic Elements (Appearing Content)

69/70

Handle Dynamic Elements

Dynamic elements appear, disappear, or change over time. Handling them requires proper wait strategies.

On the Appearing Elements page, three elements appear at different times (5s, 10s, 15s).

Task:

  1. Navigate to https://testkru.com/Interactions/AppearingElements
  2. Wait for the first element (appearing5sec) to appear
  3. Print its text
  4. Check if the second element is still hidden at this point

Expected output:

First element: Message appeared after 5 seconds
Second still hidden: true

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

  1. Wait for the five-second message with visibilityOfElementLocated() and keep the returned element.
  2. Print its text after the label First element:.
  3. Find the ten-second message and print whether it is still hidden, the negation of isDisplayed(), after the label Second 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.

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(10));
        
        // TODO: Wait for appearing5sec to become visible
        // TODO: Print its text
        // TODO: Check if appearing10sec is still not visible
        
        driver.quit();
    }
}