简体   繁体   English

使用“ExpectedConditions”等待验证不起作用

[英]Wait verify using “ExpectedConditions” does not work

UPDATED VERSION更新版本

I'm trying to find a more dynamic way to wait for elements instead of using static wait functions like Task.Event(2000).Wait();我试图找到一种更动态的方式来等待元素,而不是使用像Task.Event(2000).Wait();这样的静态等待函数Task.Event(2000).Wait();

The solution to this seems to be this:对此的解决方案似乎是这样的:

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

But when I use this function, the "ExpectedConditions" always lights up red indicating that it: "does not exist in the current context".但是当我使用这个函数时,“ExpectedConditions”总是亮红色,表示它:“在当前上下文中不存在”。

I've placed the function in one of my testcases:我已将该函数放在我的一个测试用例中:

(I am using C#/Visual Studios, Project type: Classlibrary) (我使用的是 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();
            }
       }
   }

(The xpath is a location type xpath, it is valid and works in both Selenium IDE and in Visual Studios.) (xpath 是一个位置类型 xpath,它是有效的,并且在 Selenium IDE 和 Visual Studios 中都有效。)

Anyone has any idea what I'm doing wrong?任何人都知道我做错了什么?

I have two suspicions:我有两个怀疑:

  1. My version of selenium could be too new (3.6.0.0)我的 selenium 版本可能太新 (3.6.0.0)

According to selenium.io , ExpectedCondition was last updated in 3.1.0.根据selenium.io , ExpectedCondition 上次更新是在 3.1.0 中。 Maybe it is no longer vaild.也许它不再有效。 Is there someway to check this?有没有办法检查这个?

  1. Maybe my project type is not compatible with ExpectedCondition, and therefore does not recognize it?也许我的项目类型与 ExpectedCondition 不兼容,因此无法识别它?

I've seen this before.我以前见过这个。

Make sure you've installed both the Selenium.Webdriver and Selenium.Support NuGet packages for your project.确保你已经为你的项目安装了 Selenium.Webdriver 和 Selenium.Support NuGet 包。 You'll need the Selenium.Support package to use ExpectedConditions.您需要 Selenium.Support 包才能使用 ExpectedConditions。

I resloved this way:我是这样重新喜欢的:

1: Using nuget, search for DotNetSeleniumExtras.WaitHelpers, 1:使用nuget,搜索DotNetSeleniumExtras.WaitHelpers,

2: Import that namespace into your class. 2:将该命名空间导入到您的类中。

    using SeleniumExtras.WaitHelpers

3: Then run the following code: 3:然后运行以下代码:

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

I don't understand this line:我不明白这一行:

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

How are you able to cast Until to an IWebElement since it returns bool?既然 IWebElement 返回 bool,你怎么能把它转换成一个 IWebElement? I might be missing something.我可能会遗漏一些东西。

But you might be able to bypass the error if you don't use ExpectedConditions, like so:但是,如果您不使用 ExpectedConditions,则可能可以绕过该错误,如下所示:

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

WebDriverWait takes as a parameter a function that returns a bool. WebDriverWait 将返回 bool 的函数作为参数。 You can create one within the parameters with the above code, that will return true when the element is enabled.您可以使用上述代码在参数中创建一个,当元素启用时将返回 true。

Instead of VisibilityOfAllElementsLocatedBy use elementTobeClickable method, it should work i guess而不是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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当前上下文中不存在名称“ExpectedConditions” - The name 'ExpectedConditions' does not exist in the current context Selenium C#wait.until(expectedconditions)…函数无法在屏幕上找到对象/元素 - Selenium c# wait.until(expectedconditions)… function fails to find objects/elements on screen ImplicitWait在Selenium Webdriver中等待元素似乎不起作用 - ImplicitWait does not seem to work in Selenium Webdriver to wait for elements 流利的等待是否适用于 Selenium 中的 Page Factory Web 元素? - Does fluent wait work with Page Factory Web Elements in Selenium? 使用 Bcrypt C# 验证密码无法正常工作 - Verify password won't work perfectly using Bcrypt C# Moq.Mock.Verify()是否使用identity或.Equals()比较参数? - Does Moq.Mock.Verify() compare parameters using identity or .Equals()? 使用 Microsoft.Identity.Web RequiredScope 来验证 controller 级别的范围 - 不验证 scope - Using Microsoft.Identity.Web RequiredScope to verify the scopes at the level of the controller - Does not verify scope 使用 ReaderWriterLock 时,第二个线程是否实际等待 - When using ReaderWriterLock does second thread actually wait or not SeleniumExtras.WaitHelpers.ExpectedConditions - SeleniumExtras.WaitHelpers.ExpectedConditions 流程等待退出不起作用 - Process Wait For Exit not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM