Refreshing page and locating element 2nd time causes stale element

Hi,

I have 2 similar pages with a filter and a table with the element I am looking for.
The goal is to enter the name in the filter and refresh the page until the table has the element I am looking for.

On the first page my code works flawlessly, however on the second page I keep getting a stale element after the first run of my while-loop, and I can’t figure out why.

Here is to hoping someone can find an issue in my code, the stale element is thrown when the ‘rows’-list is created.

WebElement tBody = null
String element = null	
int x = 0
int sleepCounter = 0 

loop:
while(element  ==  null) {
// WebElement tBody = null 		
TestObject to = 'Class to find the filter-input'
wait.andDLClick(to, 10)

while(tBody == null) {
	tBody = driver.findElement(By.tagName('tbody'))
}
		
List <WebElement> rows = tBody.findElements(By.tagName('tr')) // stale element get thrown here every time after the first run
							
try {
	*validation of the element using the 'rows' list created above
or if the element is not found I have a counter in place to either:
    * refresh the page and try again from with the while loop
    *  or fail the test when a threshold is reached
} catch (e) {
//e.printStackTrace()
}
		
		
}// end while loop

Taking a quick look at your code, it’s probably tBody that’s causing the staleElement error.

More precisely, it’s your reference to the element that has died. Once you refresh the page, ALL “old” references to elements are stale/gone/dead/expired.

Think of it this way, the page has been rebuilt from scratch. The browser has a completely new page loaded. The only person that sees them as “the same page” is you.

Re-get (refresh) your Text Objects/elements and your code will work fine on iterations 1..n