Stale Element when handling a table

Maybe I’m not clear at the first time. But I want to continue my test in the new page after clicking. I don’t want to back.

Change from your original post

To

Boolean iHaveClickedDelete = false
for (int i = 0; i < rows.size() && !ihaveClickedDelete; i++) {
    List<WebElement> cels = rows.get(i).findElements(By.tagName('td'))
	
    for (int j = 0; j < cels.size() && !iHaveClickedDelete; j++) {
        if (cels.get(j).getText().equalsIgnoreCase(expectedContract)) {
				wait.until(ExpectedConditions.presenceOfElementLocated(cels))
				cels.get(5).findElement(By.tagName('a')).click()
				iHaveClickedDelete = true
        }
    }
}

@danpoleary, just a reminder that you have two levels with your for loop, so you should use,
break label;
instead of just “break”.
A label needs to be placed above the first for statement, like Pizza: (add a colon after the label) and then add that label after the break statement, like
break Pizza;
Without the label, you only exit one level; with the label you exit both levels. That maybe where the StaleElementException is occurring as well.

I took the break out in my example for him. I try and avoid breaks. :slight_smile:

Okay, but you should stop your looping if you have satisfied your requirements. I forgot once and wow, what a situation.

I think so too, but I don’t know how to use break at both level.
@danpoleary I understand your solution, I tried it by using While statement, but I still have the error

You do not need the while statement.

I changed your code from original post.

The total code is:

String expectedContract = '2018'

WebDriver driver = DriverFactory.getWebDriver()

WebElement siteTable = driver.findElement(By.xpath('//table[@class="contractList widgetBodyTable"]'))

List<WebElement> rows = siteTable.findElements(By.tagName('tr'))
Boolean iHaveClickedDelete = false

for (int i = 0; i < rows.size() && !iHaveClickedDelete; i++) {
    List<WebElement> cels = rows.get(i).findElements(By.tagName('td'))
	
    for (int j = 0; j < cels.size() && !iHaveClickedDelete; j++) {
        if (cels.get(j).getText().equalsIgnoreCase(expectedContract)) {
				wait.until(ExpectedConditions.presenceOfElementLocated(cels))
				cels.get(5).findElement(By.tagName('a')).click()
				iHaveClickedDelete = true
			}
        }
    }
}
// Continue on your next page here

If the variable “iHaveClickedDelete” is still false after the outer for loop, then that means the expectedContract was not found. So check for that before assuming you are on the next page.

@danpoleary I got this error. The “iHaveClickedDelete” is greyed.

Test Cases/Handle Table FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: ihaveClickedDelete for class: Script1673139059027
	at Handle Table.run(Handle Table:72)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1673541692317.run(TempTestCase1673541692317.groovy:25)

Probably just a spelling mistake. The above variable has a lowercase “h” for “have”, whereas, Dan’s code above initialized an uppercase “H”.

@mooctacthanh Yep, my bad, the h went lowercase by accident. “iHaveClickedDelete” is what it should be. You can make that variable name whatever you wish. It was just an example.

That’ll work. Thank you for all your help