All IT Courses 50% Off
Selenium Tutorials

Understanding Stale Elements Reference Exception in Selenium 

You will (or may already have) encountered several exceptions while using the Selenium WebDriver. Depending on the type of exception, these errors stop your tests from running and let you know what went wrong. They are occasionally employed for critically important features that might not otherwise be produced. For instance, utilising the KeyboardInterrupt exception, adding a feature that allows users to quit programs using Ctrl+C just requires three lines of code. One such typical exception is the StaleElementReferenceException. Stale often denotes something that isn’t current. The StaleElementReferenceException in selenium denotes the absence of a potential earlier element.

What is StaleElementReferenceException in Selenium WebDriver?

Selenium test execution for an environment with frequent changes frequently results in StaleElementReferenceException. The exception means that a previously present element on the page is no longer there. It denotes that the element has been taken out of the page’s DOM. When an element discovered by the findElement function cannot be interacted with, the StaleElementReferenceException appears. A good Selenium online certification course needs to be able to adequately explain this idea.

Understanding Stale Elements Reference Exception in Selenium 

When can we expect a StaleElementReferenceException?

There are two common causes leading to Selenium StaleElementReferenceException which have been highlighted in Selenium documentation as well.

All IT Courses 50% Off
  • The referenced element is not attached to the DOM anymore.
  • The referenced element has been deleted permanently.

Exception due to referenced element not attached to the DOM anymore 

Let’s first examine how selenium functions. Selenium stores the reference ID of the element we are trying to discover in memory when we use the findElement() method in WebDriver. Instead of looking for the element again when you try to interact with it later, the saved reference ID is used to try to find the element.

However, there may be times when you take some action on the website and the reference to a few elements is changed or refreshed as a result. You would see StaleElementReferenceException in such circumstances.

Permanent deletion of the referenced element

Sometimes a situation may arise where the referenced element may have been permanently erased. For instance, if the user switched to another page or the page where the element is located was refreshed before the interaction with the element occurred. Another reason for deletion could be the substitution of an element with comparable properties for the element from the Javascript library. The substituted item would seem to be comparable, but due to attribute similarities, the webdriver would be unable to distinguish it. When you scrolled down to see the page’s contents (or any table listings on the page), assuming that you had already recognized an element on the page, the page immediately reloaded to load the contents. In such a scenario if you try to execute an action on the element you previously recognized you might stumble upon the StaleElementReferenceException.

How to avoid StaleElementReferenceException?

We will examine potential remedies to avoid StaleElementReferenceException now that we are aware of its sources.

Solution 1 is to reload the website.

If you encounter the StaleElementReferenceException, you can try the refresh function on the website. Try searching for the same element once you’ve refreshed.

Understanding Stale Elements Reference Exception in Selenium 

Solution 2: Employ the explicit wait

To solve the element-finding issue, we can use the Selenium webdriver explicit wait function. Now, using explicit wait can be done in one of two ways:

  • Await the element’s appearance.
  • Hold off until the element has been updated.
  • Hold off till the component is present.
  • We can perform the intended actions after waiting for the element to become available by using the explicit wait.

Wait until the element is refreshed

StaleElementReferenceException can be prevented by using the ExpectedConditions.refreshed function. The webdriver will then watch for the element to refresh and be prepared to accept its reference.

The webdriver has been instructed to wait for the element to become visible until the DOM manipulation has taken place. This is similar to the standard explicit wait approach. When we know that DOM manipulation may occur on the web page, the refreshing approach is most suited, and the webdriver would wait for the element to be refreshed within the allotted period.

Solution 3: Using POM

We locate an element using the @FindBy annotation when using the POM design pattern or the concepts of Page Factory. Every time an action is done on a web element, it helps to update the reference of that element. This becomes quite helpful in preventing StaleElementReferenceException from occurring.


As a Selenium tester, it’s important to understand the basic concept of StaleElementReferenceException. To learn more, check out the online Selenium training.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button
H2kinfosys Free Demo Online Training