c# - 等待时发生 StaleElementReferenceException

标签 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 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

最佳答案

我建议您尝试使用 ExpectedConditions,看看是否有帮助。

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

关于c# - 等待时发生 StaleElementReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845215/

相关文章:

c# - Xamarin.Forms Navigation.PushAsync 在 ContentView 中不起作用

selenium - 无法找到与 selenium 3.8.1 和 gecko 驱动程序 0.19.0 匹配的功能集

c# - 从文件名 c# 订购字符串 [] 路径

具有多态性的 C# Func<Interface>

java - while 循环出现 NoSuchElementException

java - 如何在写入文件之前关闭selenium java中的excel文件?

java - Selenium TestNG 页面对象模型框架中的问题

python - 访问工具提示selenium python后面的元素文本

c# - 列表中的字符串匹配

c++ - 什么时候适合使用 C++ 异常?