目前我正在经历一种情况。在表的正文中有三行。如果行与文本匹配,我必须对每行执行一些操作。为此,我得到了行的大小,并使用For循环和检查条件。当满足条件时,我必须执行一些操作,通过这些操作将行从网络表中删除,这符合我的预期。此外,我得到了org.openqa.selenium.StaleElementReferenceException:这里"totalOrders.get(i(.click(("当循环尝试在此处的下一行执行操作时
这是我代码的一部分:
By loading = By.xpath("//div[@class='loading-wrap']");
By orders = By.xpath("//tbody/tr"); //this retruns 3 row
public void invoiceAllStockOrder() {
eu.waitForInvisibilityOfElementLocated(loading, 10);
List<WebElement> totalOrders = eu.getElements(orders);
int rowSize =totalOrders.size();
if(rowSize == 0) {
System.out.println("No order");
}
else {
for (int i = 0; i < totalOrders.size(); i++) {
eu.waitForInvisibilityOfElementLocated(loading, 10);
totalOrders.get(i).click();//getting stale element exception here when i = 1 but there are still 2 rows left
selectInstockOrders();
invoiceOrder();
}
}
}
If the page has Javascript which automatically updates the DOM,
you should assume a StaleElementException will occur.
Can you try with the below code, I hope this will work for you
public boolean retryingFindClick(By by) {
boolean result = false;
int attempts = 0;
while(attempts < 2) {
try {
driver.findElement(by).click();
result = true;
break;
} catch(StaleElementException e) {
}
attempts++;
}
return result;
}
This will attempt to find and click the element. If the DOM changes
between the find and click, it will try again.