小编Dav*_*vid的帖子

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

更新后的版本

我试图找到一种更动态的方式来等待元素而不是使用静态等待函数 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 …

c# selenium wait visual-studio selenium-webdriver

1
推荐指数
2
解决办法
4771
查看次数

标签 统计

c# ×1

selenium ×1

selenium-webdriver ×1

visual-studio ×1

wait ×1