C# 等待时发生StaleElementReferenceException
我的测试中有C# 等待时发生StaleElementReferenceException,c#,exception,selenium,selenium-webdriver,wait,C#,Exception,Selenium,Selenium Webdriver,Wait,我的测试中有wait,如下所示: 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 fal
wait
,如下所示:
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%的时间内都能正常工作,在测试中的不同地方都失败了
错误:
消息:OpenQA.Selenium.StaleElementReferenceException:元素不存在
在缓存中找到-可能页面在查看后已更改
向上
我建议您尝试使用ExpectedConditions,看看是否有帮助
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement loaderGif = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@src='/loader.gif']")));
d
是WebElement还是WebDriver?如果是前一个,请确保在执行等待时不会更改。谢谢!自从我用了这个以后就没问题了。