StaleElementReferenceException WebDriver Exception

Before understanding StaleElementReferenceException, you have to first understand the below:

So, after understanding 'What is an Exception and the different types of Exceptions in Selenium WebDriver', you are now ready to understand StaleElementReferenceException in a detailed and practical way.

StaleElementReferenceException is one of the different WebDriver Exceptions and this Exception occurs mainly because of page navigations in between the execution of Selenium code. i.e. This exception occurs, when Selenium navigates to a different page, come backs to the same old page and performs operations on the old page. Technically, it occurs when the element defined in the Selenium code is not found in the cache memory and the Selenium code is trying to locate it. When we define an element on a page using Selenium, Selenium stores it in a cache memory. The element stored in cache memory generally gets deleted when the driver navigates away to another page during Selenium code execution. On coming back to the same old page and then trying to identify the cache removed element on the old page, we get StaleElementReferenceException as a result.

The below diagram depicts StaleElementReferenceException:

Practical Example for StaleElementReferenceExcpetion:

1. Open Firefox Browser and open as shown below:

2. Right click on the 'Selenium143' link and select 'Inspect in Firepath' option as shown below:

3. Copy the XPath generated by Firepath as shown below:

4. Define an element for 'Selenium143' link in Selenium code, using the above copied XPath as shown below:

5. Now click on the 'Selenium143' link on the page using the above defined element as shown below:

6. Execute the above Selenium code and observe that it opens '' page, defines 'Selenium143' link element, clicks on 'Selenium143' link element and navigates away to 'Selenium143' page without any error as shown below:

7. Now update the Selenium code to navigate back to the old page i.e. '' as shown below:

8. Using Selenium, now again click on the same link element which was defined in step 4 and which got removed from cache after the Selenium driver got navigated to a different page in step 5.

9. Run the above Selenium code and observe that we get 'StaleElementReferenceException' WebDriver Exception when the last statement in the code gets executed. The below is the proof:

10. Also the statements after the above exception wont be executed, as the program stops execution once the exception is occurred. i.e. The below statement in the code wont be executed to the exception in the before statement:

Handling StaleElementReferenceException :

1. We can use try catch blocks to handle the exception and also we can use StaleElementReferenceException WebDriver class in catch block to handle the exception. Observe that the exception will be handled and also the statements after the handled exception will also be executed as shown below:

2. Hover the mouse over the 'StaleElementReferenceException' error in the above image and select 'import StaleElementReferenceException org.openqa.selenium ' option from the suggested resolutions as shown below:

3. Observe that the error will be resolved and Selenium code will now look as shown below:

4. Now execute the Selenium code and observe that StaleElementReferenceException WebDriver Exception is handled and program execution didn't halted. Below is the proof:

5. Even-though the 'StaleElementReferenceException' got handled, but the defined element which got removed from the cache memory, didn't get clicked. In order to click the defined element which got removed from cache memory, we have to reassign the lost address. The best place for reassigning the lost address for the cache removed element is 'catch' block as shown below:

6. Now execute the Selenium code and observe that the element will be restored in the cache memory and element will be clicked. Below is the proof:

Hence 'StaleElementReferenceException' occurs, when Selenium performs operations on the defined elements of the page after navigating away and coming back to the page. In order to handle this exception, we have to use 'StaleElementReferenceException' WebDriver class in the catch block. And also if we want to perform operations on the earlier defined elements, even after navigating away from the page and coming back, we have to reassign the lost address of the element, so that it gets stored in the cache memory again.

No comments: