The Untold Story — Stale Element Reference Exception in Selenium

StaleElementReferenceException is one of the most intriguing exception that you would have experienced in your selenium tests. Hope this post can help you to solve this exception from reoccurring from your tests.

What is StaleElementReferenceException?

Per Selenium Webdriver documentation, the reference to an element is now “stale” — the element no longer appears on the DOM of the page. In simple words, the element that you located using the findElement method disappeared when you started interacting with it.

Let us understand some insights about how Selenium WebDriver handles this scenario:

WebDriver Internals

Way of Working — Selenium WebDriver

WebDriver Method

For example, if you like to look up for an element in the browser using Selenium WebDriver in Java, the syntax looks following:

Locate an Element using Id as Selector

API Request

Now, the java method internally calls the WebDriver API — the post request through passing the locator information in the body like below:

FindElement request posted through PostMan

In the above request, localhost points to chromedriver running as a local server in port 9515.

Note: When running your tests from your IDE like eclipse, Selenium finds the free port at runtime and that may not be the 9515, which is the default port.

API Response

The API response from the chromedriver server on a successful match will return the WebElement and the response will like below:

Response with Element Information

Here, you will find the element information with a unique identifier for every session for that requested DOM. Note: The element information will be different when the page is reloaded or navigated from different sessions.

Element information when reloaded

WebDriver Response

This Element information is received as a response from the WebDriver findElement method and cast as WebElement object.

Response from API converted as WebElement

Stale Element Reference is thrown from Browser Driver

With this information, it is evident that when you are trying to interact with the element that found few milliseconds before could have disappeared because the page got refreshed/reloaded when you interacted with click or type or something.

Due to this element information change at the DOM between calls (locate and action), the Stale Element exception is thrown by ChromeDriver (not by the Selenium WebDriver).

Stale Element Exception from Chrome Driver

In sum, the exception is thrown by ChromeDriver (not Selenium WebDriver) when the element information got changed when the page got reloaded between the locate (findElement) and action (click/sendKeys).

How do we fix this exception?

There can be multiple ways to fix this problem. You need to find the simplest and optimized option based on your problem occurrence and experience.

  1. Simple Wait

Add simple wait using Thread.sleep for few seconds (or maybe little more based on the time you think your application will complete the component) before you locate the element using findElement

2. Retry to locate again on Stale exception

Add exception handling to your action and if the exception is stale, then retry to locate the element after a simple wait for 500 milliseconds and repeat these actions until the success of action or for max allowed iterations.

3. Use DevTools to know the DOM conditions

Use dev-tools API to find to know if the DOM got reloaded between the find element and click. If got refreshed, then ask the listener to return the new element information for the next action.

Here is the Github link for more details.

Summary

The StaleElementReferenceException is one of the common exceptions and once you know why it occurred and how to fix them, you will conquer them. Every exception will teach you something unique, so keep learning.

Author, Speaker, Mentor and Coach - Test Automation & DevOps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store