top of page
Search

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

Writer's picture: Tuyen NguyenTuyen Nguyen

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.

350 views0 comments

Recent Posts

See All

Commenti


Join my mailing list

Thanks for submitting!

© 2019 by Tuyen Nguyen

© 2019 by Tuyen Nguyen

  • White LinkedIn Icon
  • lm29-5xjdbzxe9s94-mn8yw
  • White Instagram Icon
bottom of page