StaleElementReferenceException
Why This Exception Happens (Real Project Context)
StaleElementReferenceException occurs when Selenium tries to interact with a WebElement that is no longer attached to the current DOM.
This is extremely common in modern SPAs where UI elements are re-rendered, not updated.
Typical Real-World Causes
- Page refresh or navigation
- AJAX updates replacing elements
- DOM re-render by React/Angular/Vue
- Sorting, filtering, pagination
- Dynamic table refresh
The locator is usually correct — the element reference is not.
Simple Example
WebElement saveBtn = driver.findElement(By.id("save"));
// DOM refresh happens here
saveBtn.click(); // ❌ StaleElementReferenceException
Correct Handling Strategy
Re-locate the Element (Most Reliable)
wait.until(ExpectedConditions.elementToBeClickable(By.id("save"))).click();
Do not store WebElement references early.
Using ExpectedConditions.refreshed()
wait.until(ExpectedConditions.refreshed(
ExpectedConditions.visibilityOfElementLocated(By.id("profile"))
));
Useful when DOM is frequently replaced.
Retry Pattern (Controlled)
for (int i = 0; i < 2; i++) {
try {
driver.findElement(By.id("save")).click();
break;
} catch (StaleElementReferenceException e) {
// re-try
}
}
⚠️ Use retries sparingly and with limits.
What NOT to Do ❌
- Catch exception and ignore
- Add Thread.sleep
- Increase implicit wait
- Store WebElements globally
- Blind retries without logging
Page Object Best Practice
public void clickSave() {
wait.until(ExpectedConditions.elementToBeClickable(saveBtn)).click();
}
Let the page handle re-location and waits.
Common Mistakes ❌
- Treating stale exception as locator issue
- Caching WebElements
- Using @CacheLookup in dynamic pages
- Ignoring DOM refresh behavior
- Fixing with sleeps
Best Practices ✅
- Always locate elements after waits
- Use By locators inside waits
- Avoid storing WebElements as fields in dynamic pages
- Use refreshed conditions when needed
- Log stale occurrences for analysis
Interview Notes 🎯
Q: What is StaleElementReferenceException?
A: When a WebElement reference is no longer valid due to DOM changes.
Q: How do you fix it?
A: Re-locate the element after DOM update.
Q: Does increasing wait fix stale issues?
A: No.
Real-Project Tip 💡
If stale exceptions occur frequently, your UI is re-rendering aggressively — adjust locator strategy and waits.
Summary
- Stale element ≠ bad locator
- Caused by DOM replacement
- Re-locate elements correctly
- Avoid static waits and caching