Issue after clicking specific text in a table : stale element reference: element is not attached to the page document

I am able to execute the code below, and it even clicks on my expected text. But the issue is that after clicking, I get a

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

Here is my code snippet
String ExpectedPosition = findTestData(‘PositionNames’).getValue(1, 1)

WebDriver driver = DriverFactory.getWebDriver()

WebElement PositionTable = driver.findElement(By.xpath('//td[2]/table/tbody/tr/td/table'))

List<WebElement> Rows = PositionTable.findElements(By.tagName('tr'))

println('No. of rows: ' + Rows.size())


table: for (int i = 0; i < Rows.size(); i++) {
	
	List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
	
   for (int j = 0; j < Cols.size(); j++) {
	   if (Cols.get(j).getText().equalsIgnoreCase(ExpectedPosition)) {
		   
		   
		  Cols.get(j).findElement(By.tagName('a')).click()
		  WebUI.delay(5)

		  table: break
		  
		  } 
		  }
		   
		  }

One oops you have is you have two labels with the same name: table. You should change the code in the for loop from:
table: break

to become:
break table;

The idea is that you want to break out of the both inner and outer loops after you have found your item of interest. Your upper label, table, is the reference you want to break to.

See if that is your concern too.

2 Likes

Thanks that did it

using break; resolved my issue