## TL;DR
A stale element means the DOM node you found was replaced before you acted on it, which happens constantly under CI load where rendering is slower and re-renders interleave with your commands. Stop storing element references across page changes: locate the element fresh inside each attempt, wrapped in an explicit wait that retries until the action succeeds. The flake disappears because every attempt works from a live node.

```text
selenium "stale element reference: element is not attached to the page" flake under CI load
```

## Steps
1. Find where the test stores an element and uses it later: a variable holding a WebElement that is clicked or read after navigation, an AJAX update, or a re-render. Expected: you identify the stored reference that goes stale.
2. Rewrite the interaction so the lookup happens inside the attempt: use an explicit wait that finds the element and performs the action in one retry loop, so a stale reference just triggers a fresh lookup. Expected: no WebElement lives longer than a single attempt.
3. Prefer waiting on a condition tied to the new DOM (element to be clickable, text to be present) rather than a fixed sleep. Expected: the wait ends as soon as the fresh node is ready, keeping the test fast.
4. Remove page-object or helper patterns that cache elements as instance fields across actions. Expected: every action re-locates from the driver.
5. Rerun the suite under CI load several times. Expected: the stale element errors stop appearing.

## Use this when
- Selenium fails with "stale element reference" intermittently in CI
- The page updates, navigates, or re-renders between find and action
- The same test passes reliably on a fast local machine

## Not for this skill when
- The element never appears (wrong locator or missing data)
- The error happens on the very first find with no page change (locator issue)
- The runner is Cypress or Playwright, which auto-retry differently

## Variant phrasings
- selenium stale element reference flaky CI
- element is not attached to the page document selenium
- selenium stale element under load how to fix

## Why it happens
Selenium's element references point at specific DOM nodes, not at selectors. Any re-render, navigation, or partial page update detaches the old node, and the stored reference becomes invalid. Under CI load, rendering is slower, so the window between "found" and "acted" where a re-render can land grows, turning a rare local flake into a constant CI failure.

## Edge cases
- Single-page apps re-render on state changes your test triggers (typing filters a list); re-locate after each keystroke-driven change.
- IFrames: a stale reference inside a frame also needs the frame context re-entered after the re-render, not just the element re-found.
- Do not catch the stale exception and blindly retry the old reference. The retry must re-run the locator, or it will go stale again the same way.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_8Po5P7abKO9edRY1lNdu_w
