Getting Stale Element Reference Exception intermittently | Selenium Forum
M
Posted on 02/12/2015
Hi,

I have a webpage with different grids. In one of the grid, I have a radio buttons say button1 & button2 and a drop down list say list1.

On selecting particular radio button, drop down list is getting refreshed and values related to that radio button is pushed into the drop down list.
I am getting Stale Element Reference Exception intermittently while handling this part of automation.
unfortunately I can't share code and URL details for this.

can you advice some solution for this please.

M
Replied on 03/12/2015

stale element exceptions are very hard to handle.

First of all lets be clear about what a WebElement is.

A WebElement is a reference to an element in the DOM.

A StaleElementException is thrown when the element you were interacting is destroyed and then recreated. Most complex web pages these days will move things about on the fly as the user interacts with it and this requires elements in the DOM to be destroyed and recreated.

When this happens the reference to the element in the DOM that you previously had becomes stale and you are no longer able to use this reference to interact with the element in the DOM. When this happens you will need to refresh your reference, or in real world terms find the element again.


This is not a problem. If you wrap your .findElement call in a try-catch block and catch the StaleElementReferenceException , then you can loop and retry as many times as you need until it succeeds.

Here are some examples I wrote.

Another example from Selenide project:
[code:2eo8d2xj]
public static final Condition hidden = new Condition("hidden", true) {
@Override
public boolean apply(WebElement element) {
try {
return !element.isDisplayed();
} catch (StaleElementReferenceException elementHasDisappeared) {
return true;
}
}
};
[/code:2eo8d2xj]


if above doesn't work then try this blog

http://darrellgrainger.blogspot.com/2012/06/staleelementexception.html