2015-09-29 47 views
1

我在我的測試wait這樣的:StaleElementReferenceException在等待

WebDriverWait wait = new WebDriverWait(dr, TimeSpan.FromSeconds(30)); 

... 

wait.Until((d) => 
{ 
    if (d.FindElement(By.XPath("//*[@src='/loader.gif']")).Displayed) 
    { 
     System.Threading.Thread.Sleep(200); 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
}); 

我有時會StaleElementReferenceException。它在95%的時間內工作,並在測試中的不同位置失敗。

錯誤:

Message: OpenQA.Selenium.StaleElementReferenceException : Element not found in the cache - perhaps the page has changed since it was looked up

+0

是'd' WebElement還是WebDriver?如果是前者,確保在執行等待時不會改變。 – skandigraun

回答

0

我會建議您嘗試使用ExpectedConditions,看看有沒有什麼幫助。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); 
IWebElement loaderGif = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@src='/loader.gif']"))); 
+0

謝謝!自從我使用這個以後沒有問題。 – nEkis