Understanding Stale Element Reference Exception in Selenium
Last Updated :
05 Aug, 2025
Selenium is a widely used tool for interacting with web elements programmatically in web automation testing. However, one of the common challenges testers face is the Stale Element Reference Exception. This exception occurs when a previously located web element is no longer available in the DOM (Document Object Model), often caused by dynamic page updates or reloading.
Understanding why the Stale Element Reference Exception happens and learning how to handle it effectively is crucial for creating robust and reliable Selenium test scripts.
What is a Stale Element Reference Exception?
A Stale Element Reference Exception in Selenium occurs when a web element that was once found and found and interacted with is no longer valid in the Document Object Model (DOM) that means the element's reference has been become stale or outdated and it can no longer be interacted with the same way.
State Element Reference Exception typically arises in the following situations:
- Page Refresh or Navigation: If a page is refreshed reloaded or navigated away, the previously found elements may no longer exist in the DOM causing the reference to become stale.
- Dynamic Content Updates: The web pages often update the content dynamically using technologies such as AJAX, JavaScript, or DOM manipulations. These updates might replace or remove the elements and make the previously located elements invalid.
- DOM Changes: Any structural changes in DOM like inserting elements, modifying elements, or deleting elements can invalidate the reference to an existing element.
When does the Stale Element Reference Exception occur?
A Stale Element Reference Exception occurs when the element we are trying to interact with is no longer considered valid by the browser's DOM. This typically happens in two main scenarios:
- The HTML web element is no longer present on the webpage: This occurs if an element has been removed or replaced in DOM after an element was referenced. For example, if a page is refreshed or if the JavaScript is dynamically altered the DOM.
- Permanent deletion of referenced element: This happens when an element is entirely removed from DOM, often the result of JavaScript operations or the page transitions.
Why Testing on Real Devices and Browsers important?
Testing on real devices and browsers are the crucial for the ensuring quality and reliability of the web and mobile applications. The following are important for testing:
- Accurate User Experience
- Device-Specific Issues
- Browser-Specific Rendering
- Network Conditions
- Real-world Bugs
- Accurate Geolocation and GPS Testing
- Third-Party Integration
- Testing Edge Cases
- Comprehensive Bug Detection
- Compliance and Certification
How to handle Stale Element Reference Exception in Selenium?
Handling the Stale Element Reference Exception in the Selenium with the help of Java involves the implementing specific strategies. Here we can handle the stale element reference exception.
1. Use WebDriverWait
StaleElementExample.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class StaleElementExample {
public static void main(String[] args) {
WebDriver driver = // initialize your WebDriver;
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("element_id")));
element.click();
driver.quit();
}
}
- WebDriverWait is allowed to the dynamically wait for the element to be present or clickable before performing actions on it. This is help to prevent the Stale Element Reference Exception if an element state is changing.
2. Use the try-catch block
StaleElementExample.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.StaleElementReferenceException;
public class StaleElementExample {
public static void main(String[] args) {
WebDriver driver = // initialize your WebDriver;
try {
WebElement element = driver.findElement(By.id("element_id"));
element.click();
} catch (StaleElementReferenceException e) {
// Retry locating and clicking the element
WebElement element = driver.findElement(By.id("element_id"));
element.click();
}
driver.quit();
}
}
- Wrapping interactions inside the try-catch block can help handle the Stale Element Reference Exception by the catching exception and re-attempting to find and interact with an element.
3. Use Page Object Model (POM)
SamplePage.java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SamplePage {
WebDriver driver;
@FindBy(id = "element_id")
WebElement element;
public SamplePage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void clickElement() {
element.click(); // Interact with the element at the time of need
}
}
public class StaleElementExample {
public static void main(String[] args) {
WebDriver driver = // initialize your WebDriver;
SamplePage samplePage = new SamplePage(driver);
// Now, perform the action
samplePage.clickElement();
driver.quit();
}
}
- In the Page Object Model design pattern, elements are located at the time of the interaction, reducing the chance of the encountering the stale elements. POM helps the manage stale elements by avoiding the long-term storage of the web elements.
4. Refresh the Web Page
StaleElementExample.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class StaleElementExample {
public static void main(String[] args) {
WebDriver driver = // initialize your WebDriver;
// Refresh the page before locating the element
driver.navigate().refresh();
// Locate and click the element
WebElement element = driver.findElement(By.id("element_id"));
element.click();
driver.quit();
}
}
- Sometimes refreshing an entire page helps, especially when DOM has the undergone major updates or changes. It reloads the page to ensure that all the elements are fresh, avoiding the stale elements after the significant DOM changes.
Conclusion
In conclusion, the Stale Element Reference Exception in Selenium occurs when a web element becomes outdated due to DOM changes, page reloads, or dynamic content updates. Properly managing this exception by using techniques like WebDriverWait, refreshing web elements, or applying the Page Object Model (POM) can ensure that your Selenium tests are stable and reliable.
Effectively handling stale elements is key to reducing flaky tests and improving the overall reliability of your automation framework, especially when dealing with dynamic web content
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING