Sometimes, web elements change or reload on a page. This causes Selenium to lose track of them, leading to a StaleElementReferenceException. Handling this helps tests run smoothly without errors.
0
0
Handling StaleElementReferenceException in Selenium Java
Introduction
When a page refreshes or updates dynamically after locating an element.
When interacting with elements inside a section that reloads without full page reload.
When a test clicks a button that causes part of the page to reload, invalidating old element references.
When looping over elements that might change during the test execution.
When waiting for an element to be clickable but the element reloads in between.
Syntax
Selenium Java
WebElement element = driver.findElement(By.id("example")); try { element.click(); } catch (StaleElementReferenceException e) { element = driver.findElement(By.id("example")); element.click(); }
Use try-catch to catch the exception and then find the element again.
Re-finding the element refreshes the reference to the current page state.
Examples
Basic retry by catching the exception and re-finding the button before clicking again.
Selenium Java
WebElement button = driver.findElement(By.cssSelector("button.submit")); try { button.click(); } catch (StaleElementReferenceException e) { button = driver.findElement(By.cssSelector("button.submit")); button.click(); }
Retry clicking the link up to two times if the element becomes stale.
Selenium Java
int attempts = 0; while(attempts < 2) { try { WebElement link = driver.findElement(By.linkText("Next")); link.click(); break; } catch (StaleElementReferenceException e) { attempts++; } }
Sample Program
This test opens a page, clicks a button that refreshes part of the page, then tries to click a dynamic element. If the element is stale, it finds it again and clicks.
Selenium Java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.StaleElementReferenceException; public class StaleElementExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://example.com/dynamic"); WebElement dynamicElement = driver.findElement(By.id("dynamicElement")); WebElement refreshButton = driver.findElement(By.id("refresh")); refreshButton.click(); // This reloads part of the page try { dynamicElement.click(); System.out.println("Clicked dynamic element successfully."); } catch (StaleElementReferenceException e) { // Element became stale, find it again dynamicElement = driver.findElement(By.id("dynamicElement")); dynamicElement.click(); System.out.println("Clicked dynamic element after retry."); } driver.quit(); } }
OutputSuccess
Important Notes
Always re-find elements after a page or section reload to avoid stale references.
Use loops or retries carefully to avoid infinite loops.
Refreshing the element reference is key to handling this exception.
Summary
StaleElementReferenceException happens when the page changes after locating an element.
Handle it by catching the exception and re-finding the element.
Retries help make tests more stable when pages update dynamically.