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:

WebElement someElement = driver.findElement(By.xpath("Some XPath"));
someElement.sendKeys("SomeValue");
// Below line may give you StaleElementReferenceException if typing values change property of element or page refreshed
someElement.getAttribute("value");
WebElement someElement = driver.findElement(By.xpath("Some XPath"));
someElement.sendKeys("SomeValue");
// Relocating will help you to avoid StaleElementReferenceException 
someElement = driver.findElement(By.xpath("Some XPath"));
someElement.getAttribute("value");

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:

public WebElement getElement(WebDriver driver, WebElement element, By locator)
	{
		try {
			// Check visibility. If reference is not stale, it will return the same referenced. Otherwise it will go to catch.
			element.isDisplayed();
			return element;
			
			// Relocate element in catch and return
		}catch(StaleElementReferenceException e)
		{
			return driver.findElement(locator);
			
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		
	}

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

2 thoughts on “Ways of Handling StaleElementReferenceException Without PageFactory”

Leave a Reply

Your email address will not be published. Required fields are marked *