Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait until an element no longer exists in Selenium

I am testing a UI in which the user clicks a delete button and a table entry disappears. As such, I want to be able to check that the table entry no longer exists.

I have tried using ExpectedConditions.not() to invert ExpectedConditions.presenceOfElementLocated(), hoping that it would mean "expect that there is not a presence of the specified element". My code is like so:

browser.navigate().to("http://stackoverflow.com"); new WebDriverWait(browser, 1).until(         ExpectedConditions.not(                 ExpectedConditions.presenceOfElementLocated(By.id("foo")))); 

However, I found that even doing this, I get a TimeoutExpcetion caused by a NoSuchElementException saying that the element "foo" does not exist. Of course, having no such element is what I want, but I don't want an exception to be thrown.

So how can I wait until an element no longer exists? I would prefer an example that does not rely on catching an exception if at all possible (as I understand it, exceptions should be thrown for exceptional behavior).

like image 681
Thunderforge Avatar asked Mar 16 '15 17:03

Thunderforge


People also ask

How wait until element is not present in Selenium?

1 Answer. new WebDriverWait(driver, 10). until(ExpectedConditions. invisibilityOfElementLocated(locator));

How do you use wait until element is visible in Selenium?

Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.

What is the wait command in Selenium?

In automation testing, wait commands direct test execution to pause for a certain length of time before moving onto the next step. This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.

How do you check if element does not exist in Selenium?

The findElements gives an elements list. We shall count the number of elements returned by the list with the help of the size method. If the value of size is greater than 0, then the element exists and if it is lesser than 0, then the element does not exist.


Video Answer


2 Answers

You can also use -

new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(locator)); 

If you go through the source of it you can see that both NoSuchElementException and staleElementReferenceException are handled.

/**    * An expectation for checking that an element is either invisible or not    * present on the DOM.    *    * @param locator used to find the element    */   public static ExpectedCondition<Boolean> invisibilityOfElementLocated(       final By locator) {     return new ExpectedCondition<Boolean>() {       @Override       public Boolean apply(WebDriver driver) {         try {           return !(findElement(locator, driver).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;         }       } 
like image 77
Vivek Singh Avatar answered Sep 27 '22 22:09

Vivek Singh


The solution would still rely on exception-handling. And this is pretty much ok, even standard Expected Conditions rely on exceptions being thrown by findElement().

The idea is to create a custom Expected Condition:

  public static ExpectedCondition<Boolean> absenceOfElementLocated(       final By locator) {     return new ExpectedCondition<Boolean>() {       @Override       public Boolean apply(WebDriver driver) {         try {           driver.findElement(locator);           return false;         } catch (NoSuchElementException e) {           return true;         } catch (StaleElementReferenceException e) {           return true;         }       }        @Override       public String toString() {         return "element to not being present: " + locator;       }     };   } 
like image 28
alecxe Avatar answered Sep 27 '22 22:09

alecxe