我在TestNG(Java)中运行了一些selenium自动化测试,我收到CountableClosedRange
这个错误。我试图调试这个,有人可以帮我解决这个问题在多个页面上。我的测试有近2000行,我随机得到这个错误。我在代码中使用显式wait和Thread.sleep。
感谢
答案 0 :(得分:0)
您可以尝试使用FluentWait替代显式wait和Thread.sleep。 FluentWait允许您定义等待条件的最长时间,轮询间隔和要忽略的异常类型。
String xpath = "//somexpath";
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(StaleElementReferenceException.class);
WebElement someElement = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath(xpath));
}
});
答案 1 :(得分:0)
StaleElementException ,如果我们找到一个元素,之后DOM会更新,我们会尝试与该元素进行交互。
如果Javascript更新了findElement调用和click调用之间的页面,那么我们可能会得到StaleElementException。在现代网页上发生这种情况并不罕见。然而,它不会持续发生。时机必须适合发生此错误。
您可以尝试代码!
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;
}
如需更多参考,请查看教程:Stale Element Refernce