[Error] stale element reference: element is not attached to the page document

Hello,

Versions

  • Katalon 5.4
  • Chrome 67

**Problem : **I’m trying to execute Katalon script. The goal is to check if fields contains a specific value. I use a loop to browse all the table.

However, I don’t understand the error…

Moreover error not occure everytime at the same time. Sometime it occure during first loop iteration, sometimes on the third, …

Error

Test Cases/test FAILED because (of) org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

*** Element info: {Using=xpath, value=.//td[4]/a}

Code

List<WebElement> elements = driver.findElements(By.xpath('//*[@id="id1"]//tr[td/a]'))
for (element in elements) {
    ...
    if (!excelValue.equals(CustomKeywords.'datadriven.DataDriven.getValueFromExcel'('Sheet1', 'ColumName', 1))) {
        element.findElement(By.xpath('.//td[4]/a')).click()
        def webvalue = WebUI.getAttribute(findTestObject(object1), 'value')
        WebUI.verifyEqual(webvalue.contains(excelValue), true)
        WebUI.click(findTestObject(object2))
    }
}

Already tried

Thank’s for help!

Regards

This problem is due to references which are after the page refresh.

Solution found is to create the object with a for ‘loop’.

List<WebElement> elements = driver.findElements(By.xpath('//*[@id="id1"]//tr[td/a]'))
for(int i = 1; i <= elements.size(); i++) {
    WebElement element = driver.findElement(By.xpath('//*[@id="id1"]//tr[td/a][' + i + ']')) {
}