Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Ways of Handling StaleElementReferenceException Without PageFactory

Posted on 03/21/2025 By admin

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, 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.

Uncategorized

Post navigation

Previous Post: Selenium Interview Question 7 – How to Select Last Five Checkboxes
Next Post: REST Assured Tutorial 63 – How to create JSON with date fields using POJO

Related Posts

scrollBy500 – Make Selenium Easy Uncategorized
CanNotInject – Make Selenium Easy Uncategorized
DateSelected – Make Selenium Easy Uncategorized
ExceptionRegClasses – Make Selenium Easy Uncategorized
Git Tutorial 10 – Git Fetch – Download (Not Merging) Changes From Remote Repository Uncategorized
staleExc – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark