How and when to implement the updated (ExpectedCondition <T> condition) Selenium WebDriver?

I went through the methods of the ExpectedCondtions class and found one method: refreshed

I can understand that this method can be used when you get a StaleElementReferenceException , and you want to restore this element again and in such a way as to avoid a StaleElementReferenceException

My above understanding may be wrong, so I want to confirm:

  • When using refreshed ?
  • What should be the code for something part of the following code:

wait.until(ExpectedConditions.refreshed(**something**));

Can someone explain this with an example?

+7
java selenium selenium-webdriver
source share
2 answers

According to the source:

A wrapper for a condition that allows items to be updated by redrawing. This concerns the problem of conditions, which have two parts: find the element, and then check some condition on it. For these conditions, it is possible that the element is located, and then it is redrawn to the client. When this happens, {@link StaleElementReferenceException} is thrown when the second part of the condition is checked.

So basically this is a method that waits until the DOM is processed on an object.

Usually, when you execute driver.findElement , this object represents what the object is.

When the DOM manipulates and speaks after clicking a button, it adds a class to this element. If you try to perform an action on the specified element, it will throw a StaleElementReferenceException , since now the returned WebElement does not represent the updated element.

You will use refreshed when you expect the DOM to be manipulated, and you want to wait for it to be done in the DOM.

Example:

 <body> <button id="myBtn" class="" onmouseover="this.class = \"hovered\";" /> </body> // pseudo-code 1. WebElement button = driver.findElement(By.id("myBtn")); // right now, if you read the Class, it will return "" 2. button.hoverOver(); // now the class will be "hovered" 3. wait.until(ExpectedConditions.refreshed(button)); 4. button = driver.findElement(By.id("myBtn")); // by this point, the DOM manipulation should have finished since we used refreshed. 5. button.getClass(); // will now == "hovered" 

Note that if you say a button.click() on line # 3, it will throw a StaleReferenceException, since the DOM was processed at that moment.

In my years of using Selenium, I never had to use this condition, so I think this is an “extreme situation” and you probably won't even have to worry about using it. Hope this helps!

+8
source share

The refreshed method was very useful for me when trying to access a search result that was recently updated. Trying to wait for a search result, simply ExpectedConditions.elementToBeClickable(...) returns a StaleElementReferenceException . To get around this, this is a helper method that will wait and retry for a maximum of 30 seconds so that the search item is updated and clickable.

 public WebElement waitForElementToBeRefreshedAndClickable(WebDriver driver, By by) { return new WebDriverWait(driver, 30) .until(ExpectedConditions.refreshed( ExpectedConditions.elementToBeClickable(by))); } 

Then click on the result after the search:

 waitForElementToBeRefreshedAndClickable(driver, By.cssSelector("css_selector_to_search_result_link")).click(); 

Hope this was helpful to others.

+1
source share

All Articles