2017-05-05 27 views
-1

我有桌子的按钮。每行表格由3个按钮组成。我必须点击其中的一个3,然后转到其他页面查看更改。在我断言改变是好的之后,我想回到那些按钮并将它们设置为初始状态。所以在列表中我存储了这些按钮的初始状态。但是当我转到其他页面查看更改并返回时将这些按钮设置为初始状态。我得到StaleElementReferenceException。我明白为什么,但即使如果我再次初始化元素我得到同样的错误。我使用页面对象模式,并以这种方式初始化元素PageFactory.initElements(new ElementDecorator(Driver.getInstance()), this); 即使我把Thread.sleep,然后初始化元素钢我得到的错误。Selenium StaleElementReferenceException。如何绕过它

+0

的可能的复制[?如何避免“StaleElementReferenceException”中硒(http://stackoverflow.com/questions/12967541/how-to-avoid -staleelementreferenceexception-in-selenium) – JeffC

+0

请阅读[问]和[预计需要多少研究工作?](https://meta.stackoverflow.com/questions/2 61592 /堆栈溢出 - 用户期望的研究工作量)请提供您尝试过的代码和执行结果,包括任何错误消息等。还要提供页面链接和/或相关的HTML。 – JeffC

回答

0

不要使用thread.sleep,使用下面的方法返回布尔元素,当元素被找到时为true,当元素由于某种原因丢失时(或者你的xpath可能不够好),返回false。超时是等待多久才可以说,该元素是不存在(即5将等待5秒钟,然后扔找不到元素除外)

public boolean waitForElement(String elementXpath, int timeOut) { 

    try{      
    WebDriverWait wait = new WebDriverWait(driver, timeOut); 
    boolean elementPresent=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementXpath)).isDisplayed()); 

    System.out.printf("%nElement is present [T/F]..? ")+elementPresent; 
    }   
    catch(TimeoutException e1){e1.printStackTrace();elementPresent=false;}   

    return elementPresent; 
} 

还当你回到你的表的另一项建议能是之前试图找到任何元素刷新页面:

driver.navigate().refresh(); 
相关问题