当前上下文中不存在名称“ExpectedConditions”

Ama*_*han 2 c# selenium automation ui-automation selenium-webdriver

我必须在元素在 UI 中可见后执行操作,为此我使用下面的代码,但 ExpectedConditions 类抛出错误,指出名称“ExpectedConditions”在当前上下文中不存在。请建议。

public void WaitForElementLoad(By by, int timeoutInSeconds)
{
    if (timeoutInSeconds > 0)
    {
        WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(timeoutInSeconds));
        wait.Until(ExpectedConditions.ElementIsVisible(by));
    }
}
Run Code Online (Sandbox Code Playgroud)

错误 CS0234 命名空间“OpenQA.Selenium.Support.UI”中不存在类型或命名空间名称“ExpectedConditions”(是否缺少程序集引用?)

小智 9

下面的内容可以解决您的问题

确保您已为您的项目安装了Selenium.Webdriver和。Selenium.Support NuGet packages你需要Selenium.Support package使用ExpectedCondition

或者

可以这样解决:

1:使用nuget,搜索DotNetSeleniumExtras.WaitHelpers,

2:将其导入namespace到您的class.

using SeleniumExtras.WaitHelpers
Run Code Online (Sandbox Code Playgroud)

3:然后是代码示例code

 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("element ID")));
Run Code Online (Sandbox Code Playgroud)

  • @AmanKhan `ExpectedConditions` 很久以前就被标记为过时了(查看 GitHub 存储库,可能也已被删除了一段时间)。DotNetSeleniumExtras 现在提供了这些功能,但目前还缺少维护者。 (3认同)