Testing / Selenium Interview questions
How do you troubleshoot a StaleElementReferenceException in Selenium?
This exception means a previously found WebElement no longer refers to a live DOM node - usually because the page (or a portion of it, in a single-page app) re-rendered after the element was located.
The fix is almost always to re-locate the element fresh right before using it again, rather than caching a reference and reusing it across steps that might trigger a re-render in between:
// Instead of reusing a cached WebElement across steps: driver.findElement(By.id("status")).getText();
For elements known to re-render unpredictably, wrapping the lookup-and-action pair in a small retry loop that catches StaleElementReferenceException and re-fetches the locator is a common defensive pattern. It's worth distinguishing this from a genuine timing issue, though - if the element simply hasn't appeared yet, the fix is an explicit wait, not a stale-element retry loop, since retrying a lookup that was never going to succeed just delays the real failure.
More Related questions...