等待验证使用 "ExpectedConditions" 不起作用

Wait verify using "ExpectedConditions" does not work

更新版本

我正在尝试寻找一种更动态的方式来等待元素,而不是使用像 Task.Event(2000).Wait();

这样的静态等待函数

这个问题的解决方案似乎是这样的:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[2]/div/input")));

但是当我使用这个功能时,"ExpectedConditions"总是亮红色表示它:"does not exist in the current context"。

我已将该函数放入我的一个测试用例中:

(我正在使用 C#/Visual Studios,项目类型:Classlibrary)

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

    namespace ClassLibrary1
    {
        public class MyFirstTest
        {
            IWebDriver driver = new FirefoxDriver();

            [Test]
            public void WaitverifyTest()
            {
                driver.Navigate().GoToUrl("https://www.google.se/");
                driver.Manage().Window.Maximize();

                Task.Delay(4000).Wait();

                driver.FindElement(By.XPath("//div[2]/div/input")).SendKeys("Selenium");

                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[2]/div/input")));
                element.Click();

                driver.FindElement(By.XPath("//center/input")).Click();
            }
       }
   }

(xpath 是位置类型 xpath,它在 Selenium IDE 和 Visual Studios 中均有效且有效。)

有人知道我做错了什么吗?

我有两个怀疑:

  1. 我的 selenium 版本可能太新了 (3.6.0.0)

根据 selenium.io ,ExpectedCondition 最后更新于 3.1.0。也许它不再有效。有办法检查这个吗?

  1. 也许我的项目类型与 ExpectedCondition 不兼容,因此无法识别?

而不是VisibilityOfAllElementsLocatedBy使用elementTobeClickable方法,我猜它应该可以工作

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));

        driver.Navigate().GoToUrl("https://www.svt.se/");
        driver.Manage().Window.Maximize();

        wait.Until(ExpectedConditions.ElementToBeClickable(
        By.XPath("//li[4]/a")));

        driver.FindElement(By.XPath("//li[4]/a")).Click();

我不明白这一行:

IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[2]/div/input")));

您如何能够将 Until 强制转换为 IWebElement,因为它 return 是布尔值?我可能遗漏了什么。

但如果您不使用 ExpectedConditions,您也许可以绕过错误,如下所示:

wait().Until( foo => driver.FindElement(By.XPath("//div[2]/div/input")).Enabled);

WebDriverWait 将一个 return 布尔函数作为参数。您可以使用上述代码在参数中创建一个,当元素启用时,它将 return 为真。

我以前见过这个。

确保您已经为项目安装了 Selenium.Webdriver 和 Selenium.Support NuGet 包。您需要 Selenium.Support 包才能使用 ExpectedConditions。

我重新选择了这个方式:

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

2:将该命名空间导入您的 class。

    using SeleniumExtras.WaitHelpers

3:然后运行下面的代码:

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