Ways of Handling StaleElementReferenceException Without PageFactory

Hello Folks,

Previously, I had published on Handling StaleElementReferenceException using PageFactory. But many people asked how to handle it if they do not use Page Factory design pattern.

Understand StaleElementReferenceException through an image:

You would have seen so many explanations of StaleElementReferenceException. I would like to explain in very simple words here. You mostly encounter StaleElementReferenceException when you try to REUSE already located web element/s. Generally when we locate a web element or web elements using findElement and findElements and store it in a referenced variable of type WebElement or List<WebElement>, WebDriver does not relocate webelement each time when we perform action e.g. click, sendKeys etc on web element. It uses same stored reference every time. The stored reference may not be valid when page is refreshed or its attribute changes dynamically. In this case reference becomes old or stale and when WebDriver tries to perform an action using same reference, it throws StaleElementReferenceException. Note here that it will not throw noSuchElementException as WebDriver does not try to relocate web element.

Ways of Handling StaleElementReferenceException:

1. Page Factory Model: 

This is the best way to handle this exception. Page Factory does lazy initialization of web element at first. It search for web element everytime when any action is performed on it. It does not use old reference of web element to perform action on it. I have explained this already in below post:

Handling StaleElementReferenceException using Page Factory In Selenium

2. Relocating web element:

But every Selenium tester do not use PageFactory concept to manage web elements. In this case you need to handle/avoid within your framework and scripts. There is no other option other than Relocating web element. Fundamental rule is as below:

Do not REUSE already located web element. You should always find a web element before performing action on it even you already found previously and have referenced to it.

See the example below:

Official documnettaion of Selenium also says to relocate web element. You can create a reusable method which can give relocate element for you in case of above exception only.

Example below:

Hope, now you can understand and handle StaleElementReferenceException. There are some other situations as well where you may get this exception even if you use PageFactory. For example –
1. Locating a list of element and performing action on each element one by one.
2. Pagination scenarios
3. When you locate element when page is still loading.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning