Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 等待时发生StaleElementReferenceException_C#_Exception_Selenium_Selenium Webdriver_Wait - Fatal编程技术网

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?如果是前一个,请确保在执行等待时不会更改。谢谢!自从我用了这个以后就没问题了。