Selenium WebDriver StaleElementReferenceException [英] Selenium WebDriver StaleElementReferenceException
问题描述
运行我的测试时出现此错误:
org.openqa.selenium.StaleElementReferenceException:元素不再附加到DOM
I get this error when running my tests: org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
任何想法如何解决以上异常?
这发生在我的网格
中,其中有一个ref Xpath表达式是动态的
any idea on how to solve the above exception? this happen in my grid which has a ref Xpath expression which is dynamic
推荐答案
我遇到了同样的问题,找不到任何解决方案。提出一个解决方案,并将其发布在这里,希望这可以帮助有同样问题的人。我创建了一个类来处理陈旧元素,具体取决于它们的类型,cssselector,id等,并简单地调用它就像我任何其他页面对象一样。
I ran across this same problem and could not find any solutions. Came up with a solution and posting it here, hope this helps someone with the same problem. I created a class to handle stale elements depending on their type, cssselector, id, etc and simply call it like I would any other page object.
public void StaleElementHandleByID (String elementID){
int count = 0;
boolean clicked = false;
while (count < 4 || !clicked){
try {
WebElement yourSlipperyElement= driver.findElement(By.id(elementID));
yourSlipperyElement.click();
clicked = true;
} catch (StaleElementReferenceException e){
e.toString();
System.out.println("Trying to recover from a stale element :" + e.getMessage());
count = count+1;
}
}
我建议你只在你知道的元素上使用它导致WebDriver出现问题。
I'd recommend only using this on elements you know cause problems for WebDriver.
这篇关于Selenium WebDriver StaleElementReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!