Wait for element to disappear in Selenium Java with Examples

When automating web applications using Selenium, it is common to encounter elements such as loading spinners, pop-ups, or toast messages that appear temporarily and then disappear. Waiting for these elements to disappear is crucial before moving to the next step; otherwise, your script may fail due to premature actions.

In this article, we’ll explore how to wait for an element to disappear using Selenium in Java. We’ll cover different approaches and share hands-on code examples to make everything crystal clear.

Selenium provides various ways to wait for elements, such as Implicit Waits, Explicit Waits, and Fluent Waits, each serving different needs.

  • Implicit Waits: Set a default waiting time for the entire test script to find an element if it’s not immediately available.
  • Explicit Waits: Wait for a certain condition to be met before proceeding.
  • Fluent Waits: Similar to Explicit Waits but with the added ability to specify polling frequency and handle exceptions.

For our task, the Explicit Waits and Fluent Waits mechanism is best suited when waiting for an element to disappear, as it provides precise control over the condition being waited for.

For our example, we will use the elements available on the https://testkru.com/Interactions/DisappearingElements page. These elements disappear at different intervals, as demonstrated in the video below – the first disappears after 5 seconds, the second after 10 seconds, and the third after 15 seconds.

  • First, we need to locate the element using one of the available locator strategies. In this case, we’ll use the element’s id and locate it using Selenium’s findElement() method. Let’s select the second element on the page, which disappears after 10 seconds.
wait for element to disappear
// finding the element
WebElement element = driver.findElement(By.id("disappearing10sec"));
  • Next, let’s create an instance of WebDriverWait and set the timeout to 20 seconds.
WebDriverWait webDriverWait = new WebDriverWait(driver, Duration.ofSeconds(20));
  • Now, we can use the ExpectedConditions.invisibilityOf(element) condition to check whether the element has disappeared. Internally, this condition keeps polling the element.isDisplayed() method every 500 milliseconds until the element becomes invisible or the timeout is reached.
webDriverWait.until(ExpectedConditions.invisibilityOf(element));
Combining the whole code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;


public class CodekruSeleniumTest {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        // Open URL
        driver.get("https://testkru.com/Interactions/DisappearingElements");

        // finding the element
        WebElement element = driver.findElement(By.id("disappearing10sec"));

        // waiting for an element to disappear
        WebDriverWait webDriverWait = new WebDriverWait(driver, Duration.ofSeconds(20));
        webDriverWait.until(ExpectedConditions.invisibilityOf(element));

        // element disappeared
        System.out.println("Element disappeared..");

        driver.quit();
    }
}

Output –

Element disappeared..

This is how we can wait for the element to disappear using Explicit wait

Let’s give it a try. We will keep everything the same as the previous program, but this time we will set the timeout to 5 seconds. Since the element takes 10 seconds to disappear, let’s see what happens when the timeout is shorter than the disappearance time.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;


public class CodekruSeleniumTest {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        // Open URL
        driver.get("https://testkru.com/Interactions/DisappearingElements");

        // finding the element
        WebElement element = driver.findElement(By.id("disappearing10sec"));

        // waiting for an element to disappear
        WebDriverWait webDriverWait = new WebDriverWait(driver, Duration.ofSeconds(5));
        webDriverWait.until(ExpectedConditions.invisibilityOf(element));

        // element disappeared
        System.out.println("Element disappeared..");

        driver.quit();
    }
}

Output –

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for invisibility of [[ChromeDriver: chrome on mac (24521cb6f70fce9abb2c32396389517a)] -> id: disappearing10sec] (tried for 5 second(s) with 500 milliseconds interval)

As expected, we got a TimeoutException. So, keep this in mind when working with disappearing elements—always set the timeout based on how long the element is expected to remain visible.

Similarly, we can use FluentWait to check for the element’s disappearance. The key difference from WebDriverWait (explicit wait) is that FluentWait allows us to specify the polling interval as well.

So, let’s say that we want the polling interval to be 200 seconds and the timeout to be 20 seconds.

FluentWait fluentWait = new FluentWait(driver)
                                .withTimeout(Duration.ofSeconds(20))
                                 .pollingEvery(Duration.ofMillis(200));
Here’s the whole code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;

import java.time.Duration;


public class CodekruSeleniumTest {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        // Open URL
        driver.get("https://testkru.com/Interactions/DisappearingElements");

        // finding the element
        WebElement element = driver.findElement(By.id("disappearing10sec"));

        // waiting for an element to disappear
        FluentWait fluentWait = new FluentWait(driver)
                                .withTimeout(Duration.ofSeconds(20))
                                 .pollingEvery(Duration.ofMillis(200));
        fluentWait.until(ExpectedConditions.invisibilityOf(element));

        // element disappeared
        System.out.println("Element disappeared..");

        driver.quit();
    }
}

Output –

Element disappeared..

This is it. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *