Skip to content
CodeKruPro

Wait for an Element to Disappear

38/70

Wait for Element to Disappear

invisibilityOfElementLocated waits until an element is no longer visible. This is useful for waiting for loading spinners or messages to disappear.

On the Disappearing Elements page, disappearing5sec disappears after 5 seconds.

Task:

  1. Navigate to https://testkru.com/Interactions/DisappearingElements
  2. Verify disappearing5sec is initially visible
  3. Wait for it to become invisible (max 10 seconds)
  4. Print whether it disappeared

Expected output:

Initially visible: true
Disappeared: true

About this lesson

Wait until an element disappears with invisibilityOfElementLocated in Selenium Java

Learn Selenium JavaIntermediateWaits

How it works

ExpectedConditions.invisibilityOfElementLocated() holds when the element matching the locator is hidden or not in the DOM at all. Unlike the visibility conditions it returns a Boolean, true once the element is gone, rather than an element.

On Disappearing Elements, disappearing5sec is visible at load and hidden about five seconds later. It stays in the DOM after that; only its visibility changes. That is why this condition is defined as "invisible or absent": it covers both ways a page can make something go away.

A locator that never matched anything satisfies the condition at once. A typo in the id therefore makes the wait pass immediately and the test moves on too early, so confirm the element exists before waiting for it to vanish, as this task does.

About the assignment

A message on Disappearing Elements is visible when the page loads and hidden about five seconds later. You confirm it is visible first, then wait for it to disappear, and print both results.

What you will do

  1. Find the element by the id from the task, print isDisplayed() after the label Initially visible:.
  2. Create a WebDriverWait with the timeout the task gives.
  3. Pass ExpectedConditions.invisibilityOfElementLocated() with the same id to until().
  4. Print the Boolean it returns after the label Disappeared:.

Common mistakes

  • Checking visibility too late

    The first check must happen right after the page loads. Anything slow before it, such as a five-second sleep, lets the message disappear first.

  • Waiting on a mistyped id

    The condition is true for a locator that matches nothing, so the wait passes instantly and proves nothing.

  • A timeout shorter than the delay

    Waiting one second for the fifteen-second message throws TimeoutException with "to become invisible".

In a real test suite

Disappearance waits are how tests deal with loading spinners, progress bars and toast messages: wait for the spinner to go, then assert on the content behind it, and clicks stop failing with ElementClickInterceptedException.

Lesson 37 waited for a button to become clickable. Lesson 39 goes back to Appearing Elements and waits for two messages that arrive at different times.

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/DisappearingElements");
        
        // TODO: Check initial visibility of disappearing5sec
        // TODO: Wait for it to become invisible
        // TODO: Print results
        
        driver.quit();
    }
}