<table>
<tr id="tr1">
<td id="td1">1</td>
<td id="td2">2</td>
<td id="td3">3</td>
<td id="td4">4</td>
<td id="td5">5</td>
<td id="td6">6</td>
<td id="td7">7</td>
<td id="td8">8</td>
<td id="td9">9</td>
<td id="td10">10</td>
</tr>
<tr id="tr2">
<td id="td1">11</td>
<td id="td2">22</td>
</tr>
</table>
I'm retrieving the 'tr1' element using,
WebDriverWait wait = new WebDriverWait(driver, 5);
tableHeaderRow = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("tr1")));
Then I'm getting the 'td' list and iterating through the td elements.
List<WebElement> headersList = tableHeaderRow.findElements(By.tagName("td"));
System.out.println("Size :" + headersList.size()); //10
The list size is 10 here.
headersList.forEach(td -> System.out.println(td.getTagName()));
But when I'm iterating through the list, I'm getting a StaleElementReferenceException after iterating through 4 or 5 elements.
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document.
The issue is, even though the 'tr1' is loaded, the tds are not completely loaded, when iteration happens. The wait is only checking the mentioned parent element (tr1) loading.
Is there a proper way to handle this issue. Wait until the parent element and all the child elements are loaded.