【问题标题】:StaleElementReferenceException while waiting等待时出现 StaleElementReferenceException
【发布时间】:2015-09-29 13:25:30
【问题描述】:

我的测试中有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:元素不是 在缓存中找到 - 页面可能在查看后已更改 向上

【问题讨论】:

  • d 是 WebElement 还是 WebDriver?如果是前者,请确保它在执行等待时不会改变。

标签: c# exception selenium selenium-webdriver wait


【解决方案1】:

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

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

【讨论】:

  • 谢谢!自从我用了这个没问题。
猜你喜欢
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 2014-03-03
  • 2022-11-07
  • 2023-01-19
  • 1970-01-01
相关资源
最近更新 更多