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
- Find the element by the id from the task, print
isDisplayed()after the labelInitially visible:. - Create a
WebDriverWaitwith the timeout the task gives. - Pass
ExpectedConditions.invisibilityOfElementLocated()with the same id tountil(). - Print the
Booleanit returns after the labelDisappeared:.
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
TimeoutExceptionwith "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.
Related lessons
- Explicit Wait - Element VisibleWait for an element to become visible, the opposite condition.
- Explicit Wait - Element ClickableWaiting for an overlay to vanish is what makes the next click safe.
- isDisplayed() - Check Element VisibilityThe single
isDisplayed()check used before the wait.
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/DisappearingElements");
// TODO: Check initial visibility of disappearing5sec
// TODO: Wait for it to become invisible
// TODO: Print results
driver.quit();
}
}