Problem
I'm trying to execute a Katalon script. The goal is to check if the field contains a specific value. I use a loop to browse all of the table. However, I don't understand the error.
Moreover, the error does not always occur at the same time. Sometimes it occurs during the first loop iteration, sometimes on the third.
Versions
- Katalon 5.4
- Chrome 67
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}
Katalon 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))
}
}
HTML Code
<div id='id1'>
<table>
<tbody>
<tr></tr>
<tr>
<td>value 1</td>
<td>value 2</td>
<td>value 3</td>
<td><a> ... </a></td> // a link here
<td>value 5</td>
...
</tr>
<tr></tr>
<tr>
<td>value 1</td>
<td>value 2</td>
<td>value 3</td>
<td><a> ... </a></td> // a link here
<td>value 5</td>
...
</tr>
<tr></tr>
...
</tbody>
</table>
</div>
Already tried
- stale element reference: element is not attached to the page document
- http://darrellgrainger.blogspot.com/2012/06/staleelementexception.html
Thanks for your help!
Solution 1
This problem is due to references which are after the page refresh.
The solution I 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 + ']')) {
}