Stale Element Reference Exception in Selenium Webdriver | StaleElementReferenceException

Stale Element Reference Exception in Selenium Webdriver

In this post, we see causes of Stale Element Reference Exception and how to overcome StaleElementReferenceException in Selenium WebDriver.

What is StaleElementReferenceException:

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

Causes of Stale Element Reference Exception:

A stale element reference exception is thrown in one of two cases, the first being more common than the second.

The two reasons for Stale element reference are

  • The element has been deleted entirely.
  • The element is no longer attached to the DOM.

We face this stale element reference exception when the element we are interacting 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.

How To Overcome Stale Element Reference Exception in Selenium:

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

Solution 1:

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:

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

Solution 3:

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:

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.

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

Must Read: Data Driven Framework – Complete Guide

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

2 thoughts on “Stale Element Reference Exception in Selenium Webdriver | StaleElementReferenceException”

Leave a Comment

Share via
Copy link
Powered by Social Snap