【问题标题】:Selenium WebDriverWait throwing exception [duplicate]Selenium WebDriverWait抛出异常[重复]
【发布时间】:2019-07-03 02:51:11
【问题描述】:

我试图等到一个元素存在后再继续,但是使用我当前使用的方法,它的投掷元素不存在,即使在超时结束之前也是如此。

示例用法:

 using OpenQA.Selenium;
 using OpenQA.Selenium.Chrome;
 using OpenQA.Selenium.Support.UI;

 private static WebDriverWait wait = new WebDriverWait(driver, System.TimeSpan.FromSeconds(30)); // inside parent class (not method)

 //public static void Method1()

 wait.Until(driver => driver.FindElement(By.CssSelector("[foo=bar]")));

然后应该等待 30 秒或直到元素出现,但它会立即抛出 element not found 异常...

【问题讨论】:

    标签: c# selenium


    【解决方案1】:

    尝试使用ExpectedConditions 类,特别是ElementExists() 函数,建议代码:

    https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_ExpectedConditions_ElementExists.htm
    

    示例代码:

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    IWebElement element  = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("[foo=bar]")));
    

    示例输出:

    更多信息:

    【讨论】:

    • 我最终使用了这个解决方案,这是我最初尝试的,但 Support.UI 已弃用 ExpectedConditions,您现在必须安装一个名为“SeleniumExtras.WaitHelpers”的 nuget 包
    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 2022-01-16
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多