In my code I used Explicit Waits:

private WebDriverWait wait;
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

I tried these methods:


But often it fails with these kinds of exception in different lines of code:

  • OpenQA.Selenium.StaleElementReferenceException: The element reference is stale.` Either the element is no longer attached to the DOM or the page has been refreshed.

  • OpenQA.Selenium.StaleElementReferenceException: Element is no longer valid

I can't find stable solution for this problems.

Could you help me with this, please?

2 Answers 2


Try handling these little errors by writing methods that handle errors, like this (better than this but as an example):

public static void Until_Clickable(this WebDriverWait w, By what)
    catch (StaleElementReferenceException)

Then if theres a chance that particular element will get stale reference error:

private WebDriverWait wait;

wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

You need to find exact line/action that throws error though. Is it waiting until conditions ? Is it click ? Is it GetAttribute ? And write extensions for those methods.


StaleElementReferenceException tends to mean 1 of a couple different things. But it all comes down to the fact that the DOM is undergoing some change after you tell selenium to create a WebElement. Things that can cause a StaleElementReferenceException is if the node being targeted in the DOM disappears. Or if it's absolute location in the DOM changes.

The trickiest part about this error is that the cause can seemingly be completely unrelated to the piece of the app you're actively interacting with. Clicking a button that makes changes to a node any number of parents up the chain can cause an element reference to go stale.

When you tell selenium to get a web element reference, it uses the locator string you provide, whether it be css, class, id, or xpath. It creates a form of absolute path to that found element, so even if a DOM mutation doesn't necessarily break your css selector, as in, if you were to use it again, it'd still work properly, the absolute reference selenium created under the hood is just no longer accurate. So the reference has gone stale.

With that in mind. Without having lots of detail about not just your code solution, but the environment you're trying to automate, it's extremely hard to nail the exact reason you might be getting this exception.

Try taking some time to think about what you're doing just before this happens. Think about when you create each web element reference, and what mutations in the DOM happen during their life. Also consider how long you maintain these web element references before allowing them to be garbage collected. Different apps with their technology choices will justify different shelf lives of your elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.