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:
When you write the syntax to perform an action using Selenium WebDriver, it internally calls the WebDriver API associated with that method.
For example, if you like to look up for an element in the browser using Selenium WebDriver in Java, the syntax looks following:
Now, the java method internally calls the WebDriver API — the post request through passing the locator information in the body like below:
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.
The API response from the chromedriver server on a successful match will return the WebElement and the response will like below:
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.
This Element information is received as a response from the WebDriver findElement method and cast as WebElement object.
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).
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.
- 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.
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.