我正在使用 Java 实现大量 Selenium 测试 - 有时,我的测试由于以下原因失败StaleElementReferenceException https://developer.mozilla.org/en-US/docs/Web/WebDriver/Errors/StaleElementReference.
您能否建议一些使测试更加稳定的方法?
如果页面上发生的 DOM 操作暂时导致元素无法访问,则可能会发生这种情况。为了允许这些情况,您可以在最终抛出异常之前尝试在循环中多次访问该元素。
Try 这个优秀的解决方案来自 darrelgrainger.blogspot.com http://darrellgrainger.blogspot.com/2012/06/staleelementexception.html:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)