Quick revision
The short answer
A WebElement is a reference to one specific node in the page's DOM. When the filter makes the framework re-render the table, that node is removed and a new, identical-looking node takes its place, so our old reference points at nothing and row.click() throws StaleElementReferenceException. The correct step waits for the re-render to finish, for example with ExpectedConditions.stalenessOf(oldRow), and then locates the row again; page objects should keep By locators and look elements up when they need them.
What to remember
In this answer, we saw that a WebElement is a reference to one DOM node, not a search, and that a framework re-render replaces nodes even when the new ones look identical, which makes old references stale. We fixed the step by waiting for the re-render with stalenessOf and then locating the row again, we shaped page objects to store By locators and look elements up inside each method, and we saw why blind retries on the exception hide timing problems instead of fixing them.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A WebElement is a reference to one specific node in the page's DOM. When the filter makes the framework re-render the table, that node is removed and a new, identical-looking node takes its place, so our old reference points at nothing and row.click() throws StaleElementReferenceException. The correct step waits for the re-render to finish, for example with ExpectedConditions.stalenessOf(oldRow), and then locates the row again; page objects should keep By locators and look elements up when they need them.
Why this question is asked
StaleElementReferenceException is one of the first Selenium errors that makes people distrust the tool, because the element is plainly visible on the screen when the test says it is gone. Interviewers ask this question because the explanation shows whether we understand what a WebElement actually is, and whether we fix timing problems with real conditions or with blind retries. In this answer, we will recap the DOM and WebElement references, reproduce the failure, explain why the reference goes stale, write the step correctly, and see how page objects should be shaped to avoid it.
1A Quick Recap of the DOM and WebElement
The DOM, Document Object Model, is the browser's tree of nodes that represents the page: a table node contains row nodes, which contain cell nodes. JavaScript frameworks such as React, Angular or Vue build and rebuild parts of this tree as the application's data changes.
When we call driver.findElement(By...), the browser finds a matching node and the driver gives Selenium an opaque id for that exact node. A WebElement in our Java code is just that id. It does not store a locator and it does not search again. Every later call, such as click() or getText(), sends the id back and asks the driver to act on that same node.
So a WebElement is like a pointer to one object, not like a search query. That difference is the whole story of this exception.
2Reproducing the Problem
Here is a step that looks reasonable. It finds a row, applies a filter, and then clicks the row it found earlier.
Notice that row 1042 is still in the filtered table, with the same text and the same data-testid. To a person looking at the screen, nothing happened to it. To the driver, the node behind our reference no longer exists.
3Why the Reference Goes Stale
When the filter changes, the framework does not edit the existing rows one by one. It often throws away the table body and creates new row nodes from the new data, or replaces rows because their keys or positions changed. The new row 1042 is a different node that happens to look exactly like the old one.
Our row variable still holds the id of the old node, which has been detached from the document. When we call click(), the driver looks up that id, finds a node that is no longer attached, and reports it as stale. Selenium does not try to find a replacement, because it has no locator to search with; it only ever had the node's id.
So the error is not a Selenium bug and it is not random. It is a precise signal that we held a reference across a change in the page.
7 messages in order. Play them, or step through them one at a time.
Node B looks identical, but the test still points at node A.
4Writing the Step Correctly
The fix has two parts. First, wait until the re-render has actually happened, so we do not find the old row again a moment before it disappears. ExpectedConditions.stalenessOf(element) is made for this: it waits until the element we pass is no longer attached to the page. Second, locate the row again after the change.
Notice why the staleness wait matters. Without it, findElement right after typing could still find node A, because the request for filtered data has not returned yet, and we would be back to the same error a few milliseconds later. If the application shows a loading indicator or a result count, waiting for that to change is just as good, and sometimes clearer. The general rule is that we wait for a meaningful application state and then look the element up fresh.
5Shaping Page Objects So This Cannot Happen
Many stale element errors come from page objects that find elements in their constructor and store them in WebElement fields. Those fields are captured when the page loads, and every later re-render turns them stale. The better shape is to store By locators as fields and call findElement inside each method, at the moment the element is needed.
Now the test reads ordersPage.filterBy("Open").openOrder("1042"), and neither method holds an element longer than one action. The wait for the re-render lives in the page object, so every test that filters gets it for free.
6Why Blind Retries Are the Wrong Fix
A common shortcut is to catch StaleElementReferenceException and try again, sometimes in a generic retry helper used everywhere. It often makes the test pass, but it hides what is going on. A retry right after the error may find the old node again if the re-render has not finished, or it may click a row in a half-updated table, so the test checks a state the user never sees. It also hides real bugs, such as a table that re-renders three times for one filter change.
Waiting for a condition, the old row going stale, a spinner disappearing, a count updating, documents what the application is supposed to do and fails with a clear timeout when it does not.
7Follow-up questions
Why can PageFactory proxies still go stale?
PageFactory fields annotated with @FindBy are proxies: by default, each method call on the field runs the locator again, so a plain proxy field usually does not go stale between actions. But it can still fail in three ways. If the field is marked @CacheLookup, the element is found once and cached, so it behaves like a stored WebElement. If the DOM is replaced between the moment the proxy finds the element and the moment the action runs, a single call can still hit a stale node. And for a List<WebElement> field, the list is re-found when we access it, but the elements inside it are ordinary references, so holding one of them across a re-render goes stale exactly like our row.
Does stalenessOf work if the table is updated in place?
No. If the framework only changes the text of existing nodes, the old node is never detached, so stalenessOf waits until the timeout. In that case we wait for the content instead, for example the result count text changing or a specific row appearing, and the old reference is not stale anyway.
What about iframes and navigation?
Navigating to another page or reloading always detaches every node, so every WebElement from the old page is stale. Switching into an iframe does not make elements stale, but elements from the main document cannot be used until we switch back. The same rule applies: locate elements in the context where we use them.
8Conclusion
In this answer, we saw that a WebElement is a reference to one DOM node, not a search, and that a framework re-render replaces nodes even when the new ones look identical, which makes old references stale. We fixed the step by waiting for the re-render with stalenessOf and then locating the row again, we shaped page objects to store By locators and look elements up inside each method, and we saw why blind retries on the exception hide timing problems instead of fixing them.
Open with
A WebElement in Selenium is a reference to one specific DOM node, identified by an id the driver hands back from findElement. It does not remember the locator.
Then, if they want more
When I apply the filter, the front-end framework re-renders the table and replaces the row nodes, so even though row 1042 is still on the screen with the same text, it is a different node. My variable still points at the old, detached node, and click throws StaleElementReferenceException. To write the step correctly, I do two things.
I wait for the re-render to actually happen, for example ExpectedConditions.stalenessOf on the old row, or a loading indicator or result count changing. Without that wait, I might find the old row again a moment before it disappears. Then I locate the row again with its By locator, ideally waiting for it to be clickable, and click the fresh element.
In page objects, I store By locators as fields and call findElement inside each method, instead of finding WebElements in the constructor, because those fields go stale on every re-render. The waiting goes into the page object method, such as filterBy, so tests stay simple.
I would not wrap actions in a generic retry on the exception, because that can act on a half-updated page and hides real problems like duplicate renders; I prefer to wait for a meaningful condition.
PageFactory proxies re-find on each call, but they can still go stale with CacheLookup, in a race inside one call, or through elements taken from a List field.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A test finds a row in a table, the page re-renders after a filter is applied, and the...": https://scrivio-interview-library.netlify.app/java/ui-testing/217995a6f5/