SELENIUM BASICS:


SELENIUM WEBDRIVER:


TESTNG:


REPORTS:


BUILD & CONTINUOUS INTEGRATION TOOLS:


CROSS BROWSER TESTING:


MOBILE TESTING:


AUTOMATION FRAMEWORKS:


API Testing:


INTERVIEW QUESTIONS:


SAMPLE RESUME:

What is Stale Element Reference Exception in Selenium Webdriver & How To Fix It

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

stale element reference exception

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.

StaleElementReferenceException

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

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’

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:

Get our latest blog posts delivered to your inbox

Subscribe and get popular blog posts about software testing industry.

Rajkumar

3 thoughts on “What is Stale Element Reference Exception in Selenium Webdriver & How To Fix It”

  1. Rajkumar
    Solution 1 helped me. Thanks a lot.

  2. I fixed it by adding a wait of a few seconds before finding the element.

  3. How about:

    new WebDriverWait(driver, timeout)
    .ignoring(StaleElementReferenceException.class)
    .until((WebDriver d) -> {
    d.findElement(By.id(“checkoutLink”)).click();
    return true;
    });

Leave a Comment

Share via
Copy link
Powered by Social Snap