2017-05-05 63 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(); 
相關問題