top of page
Search
Writer's pictureTuyen Nguyen

StaleElementReferenceException - What it is and how we can solve it with refresh()

Updated: Dec 27, 2022

How we get StaleElementReferenceException?


For example when you have a text field element that is manipulated by the application and it's redrawn. Basically, you might get StaleElementReferenceException since WebDriver invokes findElement() method while the DOM has manipulated. At that time, it saves the reference to the object. If the object is redrawn, the reference to the object is no longer accessible. So it will throw StaleElementReferenceException since now the selected WebElement returned now does not represent the updated element.


ExpectedConditions.refresh()

  1. Basically, a method that waits until a DOM manipulation is finished on an element/object.

  2. refresh() accepts ExpectedCondition as an argument.

Example:

  1. button.hoverOver(); // now the class will be "hovered"

  2. wait.until(ExpectedConditions.refreshed(button));

  3. button = driver.findElement(By.css("myBtn")); // by this point, the DOM manipulation should have finished since we used refreshed.

  4. button.getClass(); // will now == "hovered"

Note that: if we perform a button.click() at line #3, it will throw StaleElementReferenceException since the DOM has been manipulated at this point.

338 views0 comments

Comments


bottom of page