0
0
Selenium Javatesting~10 mins

Handling StaleElementReferenceException in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks how to handle the StaleElementReferenceException in Selenium Java. It verifies that the test retries finding an element if it becomes stale after a page update.

Test Code - JUnit 5 with Selenium WebDriver
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;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class StaleElementHandlingTest {
    WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/dynamic");
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testHandleStaleElement() {
        WebElement dynamicText = driver.findElement(By.id("dynamic-text"));
        WebElement button = driver.findElement(By.id("refresh-btn"));
        button.click();

        int attempts = 0;
        while (attempts < 3) {
            try {
                String text = dynamicText.getText();
                assertEquals("Updated Text", text);
                break;
            } catch (StaleElementReferenceException e) {
                dynamicText = driver.findElement(By.id("dynamic-text"));
                attempts++;
            }
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser opened at https://example.com/dynamic-PASS
2Find element with id 'refresh-btn'Button element located on page-PASS
3Click the 'refresh-btn' button to update page contentPage content starts updating dynamically-PASS
4Try to find element with id 'dynamic-text' and get its textElement may be stale due to page updateCheck if element text equals 'Updated Text'FAIL
5Catch StaleElementReferenceException and retry finding elementElement reference refreshed after retryCheck if element text equals 'Updated Text'PASS
6Assertion passes confirming text is 'Updated Text'Test confirms updated page contentassertEquals("Updated Text", text)PASS
7Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: The element 'dynamic-text' remains stale after all retry attempts
Execution Trace Quiz - 3 Questions
Test your understanding
What causes the StaleElementReferenceException in this test?
AThe element locator is incorrect
BThe element was removed or refreshed after the page update
CThe browser failed to open the page
DThe assertion text is wrong
Key Result
Always re-locate web elements after page updates to avoid stale element exceptions. Use retry logic or explicit waits to handle dynamic page changes gracefully.