首页 > 解决方案 > 询问预期条件

问题描述

   public static IWebElement WaitGetElement(IWebDriver driver, By by, int timeoutInSeconds, bool checkIsVisible = false)
    {
        WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds((double)timeoutInSeconds));
        IWebElement result;
        try
        {
            if (checkIsVisible)
            {
                result = webDriverWait.Until<IWebElement>(ExpectedConditions.ElementIsVisible(by));
            }
            else
            {
                result = webDriverWait.Until<IWebElement>(ExpectedConditions.ElementExists(by));
            }
        }
        catch (NoSuchElementException)
        {
            result = null;
        }
        catch (WebDriverTimeoutException)
        {
            result = null;
        }
        catch (TimeoutException)
        {
            result = null;
        }
        return result;
    }

这是我的代码。它给了我错误:当前上下文中不存在名称“ExpectedConditions”。你能帮助我吗 。

标签: c#seleniumselenium-webdriver

解决方案


我认为您缺少这个特殊的 Nuget 包“SeleniumExtras.WaitHelpers.ExpectedConditions” https://www.nuget.org/packages/DotNetSeleniumExtras.WaitHelpers/3.11.0

您可以从 nuget 库或提供的链接下载它。稍后,正如一些评论告诉您必须在代码中添加“使用”。

我希望这有帮助


推荐阅读