C# Selenium 'ExpectedConditions is obsolete'



当尝试使用ExpectConditions显式等待元素变得可见时,Visual Studio警告我它现在已经过时,很快就会从Selenium中删除。

当前/新方法是什么来达到相同的结果?

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("content-section")));

如何使用最新版本的Selenium解决此问题。

使用 NuGet 搜索 DotNetSeleniumExtras.WaitHelpers,并将该命名空间导入到类中。现在您可以执行此操作:

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("content-section")));

IDE中的警告将消失。

如果您不想下载额外的 NuGet 包,则很容易声明自己的函数(或条件),尤其是使用 lambda 表达式,例如

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(condition =>
{
try
{
var elementToBeDisplayed = driver.FindElement(By.Id("content-section"));
return elementToBeDisplayed.Displayed;
}
catch (StaleElementReferenceException)
{
return false;
}
catch (NoSuchElementException)
{
return false;
}
});

这也是非常通用的,因为现在可以评估任何类型的布尔表达式。

这很简单。只需更改 Wait.until(ExpectConditions.ElementIsVisible(By.Id("content-section")));

Wait.Until(c => c.FindElement(By.Id("content-section")));

更改为匿名函数的答案是最正确的。或者编写你自己的类,你需要的,等待条件。

在上述显式方案中使用匿名函数的示例如下所示:

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
var element = wait.Until(() =>
{
var e = Driver.FindElement(By.Id("content-section"));
if(e.Displayed)
return e;
});

此时,函数本身可以在解决方案中的某个类中自行关闭,您可以调用该类。这样做的好处是您可以根据需要进行修改;我见过几个案例,制作非常糟糕的网站最终破坏了ExpectConditions的工作方式,而这可以通过团队编写我们自己的函数来解决。

根据 C# 贡献者:

关于预期条件,同样,这是一个补充 在 .NET 中创建完全是因为"Java 拥有它"。 当时 在Java中创建了ExpectConditions类,用于创建语法 lambda 函数(或类似 lambda 函数的东西)特别 晦涩难懂。在这种情况下,帮助程序类 对于Java绑定来说很有意义。但是,C#不是Java。在 C# 中, 用于创建 lambda 函数的语法("匿名方法"在 Microsoft文档的语言)已被 C# 很好地理解 开发人员多年,并且是其武器库中的标准工具。

在这种情况下,代码冗长的问题确实有一些优点,但是 由于等待条件很少是一刀切的,所以会很多 更清洁的方法,让用户开发自己的条件类 有他们感兴趣的等待条件。然而,这是 用户厌恶的东西。此外,想到 特定等待条件实现的"标准"集合 表面上似乎是个好主意,但有很多 用户希望任何给定条件工作方式的变化。有一个 收集等待条件可能是一件好事,但硒 项目不是它的地方。

硒贡献者的咆哮

根据 Rob F. 的回答,我在项目中添加了扩展方法。(实际上我添加了两个,WaitUntilVisible(...)WaitUntilClickable(...)

它们返回元素,而不是布尔值,因此它更像是Wait.Until(ExpectedConditions...)

// use: element = driver.WaitUntilVisible(By.XPath("//input[@value='Save']"));
public static IWebElement WaitUntilVisible(
this IWebDriver driver,
By itemSpecifier,
int secondsTimeout = 10)
{
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, secondsTimeout));
var element = wait.Until<IWebElement>(driver =>
{
try
{
var elementToBeDisplayed = driver.FindElement(itemSpecifier);
if(elementToBeDisplayed.Displayed)
{
return elementToBeDisplayed;
}
return null;
}
catch (StaleElementReferenceException)
{
return null;
}
catch (NoSuchElementException)
{
return null;
}
});
return element;
}

NuGet 是必需的 - DotNetSeleniumExtras.WaitHelpers

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("")));

我刚刚演示了元素可单击事件。同样,其他事件可以与所需的参数一起使用。

以下 C# 代码对我有用:

new WebDriverWait(webDriver, TimeSpan.FromSeconds(10)).Until(c => c.FindElement(By.Id("name")));

检查您安装了哪个版本的 Selenium.Support 和 Selenium.WebDriver NuGet 包。

我现在在最新版本 3.11.2 上遇到了同样的问题。我降级到3.10.0并解决了问题。

可以使用 NuGet 包 Gravity.Core - 它由 Gravity API 社区维护,它包含的不仅仅是 ExpectConditions 类。

如何使用

  1. 使用 NuGet 包管理器下载 NuGet。
  2. 添加using OpenQA.Selenium.Extensions

你可以像这样导入一个库

using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;

然后警告将消失。

最新更新