How to Resolve the StaleElementReferenceException in Selenium?

17    Asked by ClareMatthews in QA Testing , Asked on Dec 15, 2024

StaleElementReference Exception occurs in selenium while performing operations on a DOM object which was valid at one point of time, but now is no longer valid due to the change in the attributes in the DOM. For example, it might be because the element is reattached to the DOM, the page gets refreshed, or even the parent element of the attachment is modified. How do I manage this exception or redress it along with the test execution in order that accuracy is guaranteed by the test?

Answered by Damini das

Causes of StaleElementReferenceException:

Page Refresh or Navigation: If the page is refreshed or navigated, previously located elements become stale.

DOM Updates: Dynamic pages (e.g., those using JavaScript frameworks like React, Angular) may update or reload elements, making previous references invalid.

Element Removal: An element might be temporarily removed and re-added to the DOM.

Solutions to Avoid StaleElementReferenceException:

1. Relocate the Element

When a StaleElementReferenceException is encountered, re-find the element to get a fresh reference.

Example:

try {

    WebElement element = driver.findElement(By.id("elementID"));

    element.click();

} catch (StaleElementReferenceException e) {

    // Re-locate the element and retry

    WebElement element = driver.findElement(By.id("elementID"));

    element.click();

}

2. Use Explicit Waits

Wait for the element to become stable before interacting with it, especially on dynamic pages.

Example:

import org.openqa.selenium.support.ui.WebDriverWait;

import org.openqa.selenium.support.ui.ExpectedConditions;

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementID")));

element.click();

3. Avoid Stale Elements in Loops

When interacting with elements inside a loop, re-fetch the element during each iteration instead of reusing the same reference.

Example:

List elements = driver.findElements(By.className("items"));

for (int i = 0; i < elements>

    elements = driver.findElements(By.className("items")); // Re-fetch the list

    elements.get(i).click();

}

4. Handle Page or DOM Reload

If you expect the DOM to reload or the page to refresh, wait for the element to reappear.

Example:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementID")));

WebElement element = driver.findElement(By.id("elementID"));

element.click();

5. Wait for DOM Stability

Custom waits can help wait for the DOM to stabilize before locating elements.

Example:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(driver -> {

    try {

        driver.findElement(By.id("elementID"));

        return true; // Element is present

    } catch (StaleElementReferenceException e) {

        return false; // DOM is not stable

    }

});

6. Use Try-Catch Blocks for Retrying

Implement a retry mechanism for transient StaleElementReferenceException scenarios.

Example:

int attempts = 0;

while (attempts < 3>

    try {

        WebElement element = driver.findElement(By.id("elementID"));

        element.click();

        break; // Exit loop if successful

    } catch (StaleElementReferenceException e) {

        attempts++;

    }

}

Best Practices:

  1. Minimize DOM Interactions: Avoid unnecessary interactions or reloads that can lead to DOM changes.
  2. Use Stable Locators: Ensure locators (e.g., IDs, class names) are reliable and unlikely to change during execution.
  3. Debug DOM Changes: Use browser developer tools to identify dynamic behaviors that could cause elements to reload.

By implementing these strategies, you can effectively handle and avoid StaleElementReferenceException in Selenium.



Your Answer

Interviews

Parent Categories