Element is no longer attached to the DOM | Selenium Forum
M
Posted on 23/10/2015
The test was running fine from last 2 months and today got the following issue, could any one help me in solving this type of issues.
Test method xxxx threw exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> OpenQA.Selenium.StaleElementReferenceException: Element is no longer attached to the DOM
TestCleanup method xxxx threw exception.

M
Replied on 23/10/2015

[color=#FF0000:1l80vn58]OpenQA.Selenium.StaleElementReferenceException: [/color:1l80vn58]

it is a very common exception take a look at this q/a

http://stackoverflow.com/questions/16166261/selenium-webdriver-stale-element-reference-exception


M
Replied on 01/11/2015

Hi
Thanks for you reply, but there are many solutions and its confusing which one to use, could you suggest the proper answer please.
I hope if the element is no longer attached to the DOM and as said in the post it will be re attached, so, is it better to wait until the element is visible (I am using C# with selenium). I hope the logic of re-attaching is true as the test is failing only some times. Please correct me if I am wrong.

Thanks


M
Replied on 01/11/2015

StaleElementException are one of the difficult ones to handle

from http://stackoverflow.com/questions/16166261/selenium-webdriver-stale-element-reference-exception

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.



public static final Condition hidden = new Condition("hidden", true) {
@Override
public boolean apply(WebElement element) {
try {
return !element.isDisplayed();
} catch (StaleElementReferenceException elementHasDisappeared) {
return true;
}
}
};