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.
When can we expect a StaleElementReferenceException?
There are two common causes leading to Selenium StaleElementReferenceException which have been highlighted in Selenium documentation as well.
- 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
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.
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.