public static ExpectedCondition < Boolean > invisibilityOfElementLocated ( final By locator ) { return new ExpectedCondition < Boolean > ( ) { @ Override public Boolean apply ( WebDriver driver ) { try { return ! ( driver . findElement ( locator ) . isDisplayed ( ) ) ; } catch ( NoSuchElementException e ) { // Returns true because the element is not present in DOM. The // try block checks if the element is present but is invisible. return true ; } catch ( StaleElementReferenceException e ) { // Returns true because stale element reference implies that element // is no longer visible. return true ; } } @ Override public String toString ( ) { return "element to no longer be visible: " + locator ; } } ; }
An expectation for checking that an element is either invisible or not present on the DOM .