What does StaleElementReferenceException mean?
Options
- The element was re-rendered; find it again (Correct option)
- The browser is out of date and needs an update
- The driver executable is missing from the system
- The element is hidden behind another element
Answer
The element you found earlier was removed or re-rendered in the DOM, so the stored reference is no longer valid; find it again (and wait properly)
Why
Selenium keeps a reference to a DOM element. If the page re-renders (React updates, AJAX refresh, navigation), the old element is replaced by a new one even if it looks identical, and the old reference becomes stale.
Fix: re-locate the element right before using it, do not store WebElement objects for long, and wait for the page to settle (explicit waits). Page objects that look up elements on each action avoid this.
Remember
Stale = the DOM element was replaced; locate it again.