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

Dav*_*vid 1 c# selenium wait visual-studio selenium-webdriver

更新后的版本

我试图找到一种更动态的方式来等待元素而不是使用静态等待函数 Task.Event(2000).Wait();

对此的解决方案似乎是这样的:

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

但是当我使用这个函数时,"ExpectedConditions"总是亮起红色,表示它:"当前上下文中不存在".

我把这个函数放在我的一个测试用例中:

(我使用的是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();
            }
       }
   }
Run Code Online (Sandbox Code Playgroud)

(xpath是一个位置类型xpath,它是有效的,可以在Selenium IDE和Visual Studios中使用.)

任何人都知道我做错了什么?

我有两个怀疑:

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

根据selenium.io,ExpectedCondition最后更新于3.1.0.也许它不再是虚荣的.有没有办法检查这个?

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

小智 13

我以前见过这个.

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

  • @Sky Luke,因为 ExpectedConditions 现在已被弃用,并且不再是 Selenium 支持的一部分,您可以更改您的回复吗? (3认同)

小智 6

我重新喜欢这样:

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

2:将该命名空间导入到您的类中。

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

3:然后运行以下代码:

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