# Stale element reference exception

Classification: selector-drift
Frameworks: Selenium
Canonical: https://automationtester.in/failures/stale-element-reference-exception

## Recognizable error

```text
StaleElementReferenceException: stale element reference
```

## What it means

Selenium is holding a reference to a DOM node that the application replaced or detached after it was located.

## Ranked causes, checks, and fixes

### 1. The framework re-rendered the component

React, Vue, or another UI layer replaced the node after state changed.

Diagnostic: Check whether the failure follows navigation, filtering, or a network response.

Fix: Locate the element immediately before the interaction instead of caching WebElement references.

### 2. The document or frame changed

Navigation or frame replacement invalidated every reference from the previous document.

Diagnostic: Confirm the active URL and frame before the failed action.

Fix: Wait for navigation or frame attachment, switch context, and locate again.

### 3. A polling loop reuses the same element

Retry code may repeatedly act on a reference that cannot become valid again.

Diagnostic: Inspect whether the retry callback captures a WebElement outside the loop.

Fix: Put the locator lookup inside the wait or retry callback.

## Prevention

- Store locators, not element instances.
- Wait on state changes, not sleep durations.
- Keep frame transitions explicit.

## Primary references

- [Selenium stale element errors](https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#stale-element-reference-exception)

Triage this failure locally: https://automationtester.in/tools/test-failure-triage