In this Selenium Tutorial series , our next post is on the causes of Stale Element Reference Exception, how to overcome StaleElementReferenceException in Selenium WebDriver, and the following

What is StaleElementReferenceException in Selenium Webdriver

Stale means old, decayed, no longer fresh. Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.

TestNG Exceptions

What are the Causes of StaleElement Exception

We face this stale element reference exception when the element we are interacting with is destroyed and then recreated again. When this happens the reference of the element in the DOM becomes stale. Hence we are not able to get the reference to the element.

A stale element reference exception is a WebDriver error that is thrown in the following two cases.

Cause 1: The referenced web element has been deleted completely. 

Assume that there is an element in a web page and the page that the web element was part of has been refreshed or the selenium script navigated away to another web page. This causes any subsequent interaction with the element to fail with the stale element reference exception.

Cause 2: The referenced element is no longer attached to the DOM

Assume a document node is removed from the DOM, its element reference will be invalidated. This causes any subsequent interaction with the element to fail with the stale element reference exception.

Out of the above two causes, the first case is more common than the second case. 

How To Overcome Stale Element Reference Exception in Selenium:

Here, I will show you some ways to overcome StaleElementReferenceException. You could also refer to the official Selenium post

Solution 1: Refreshing the web page

You could refresh the page and try again for the same element.

Assume you are trying to click on a link and getting the stale element exception.

Sample code to overcome the issue

driver.findElement(By.xpath(“xpath here”)).click();

Solution 2: Using Try Catch Block

If an element is not attached to DOM then you could try using ‘try-catch block’ within ‘for loop’

// Using for loop, it tries for 3 times.
// If the element is located for the first time then it breaks from the for loop nad comeout of the loop
for(int i=0; i<=2;i++){
     driver.findElement(By.xpath(“xpath here”)).click();
  catch(Exception e){

Solution 3: Using ExpectedConditions.refreshed

Wait for the element till it gets available


Use ExpectedConditions.refreshed to avoid StaleElementReferenceException and retrieve the element again. This method updates the element by redrawing it and we can access the referenced element.


Solution 4: Using POM

We can handle Stale Element Reference Exception by using POM.

Must Read: Page Object Model Design Pattern

We could avoid StaleElementException using POM. In POM, we use initElements() method which loads the element but it won’t initialize elements. initElements() takes latest address. It initializes during run time when we try to perform any action on an element. This process is also known as Lazy Initialization.

In Conclusion:

The StaleElementReferenceException is a very common exception that we face in our Selenium tests. I hope now you learned what it is and how to fix it. 

If you find any other solution to overcome StaleElementException, you could post your solution in the comments section below.

Related Posts:

  • How Much Java is Required To Learn Selenium
  • Explain Java Main Method – public static void main(String [] args)
  • Why WebDriver Driver = New Firefox Driver
  • Explain Selenium Architecture
  • Types of Selenium Automation Frameworks
  • Data-Driven Framework – Complete Guide
  • Selenium Interview Questions
  • Best Selenium Alternatives
How To Fix A Stale Element Exception?

To fix this Stale Element error we use any of the following fixes
1. To refresh the page
2. Using try-catch
3. Using ExpectedConditions.refreshed
4. Using POM

What is the list of exceptions in Selenium?

Refer to this to learn more about exceptions in Selenium WebDriver .

How does Python handle stale element exception in selenium?

The try and except block in Python is used to handle stale element exception in selenium.