【问题标题】:Can WebDriverWait be used on an element that has already been defined?WebDriverWait 可以用在已经定义好的元素上吗?
【发布时间】:2019-06-25 10:54:09
【问题描述】:

我正在构建一个使用页面对象模型 (POM) 的自动化框架,并有一个名为 HomePage 的类文件。

我已经使用 :

声明了元素
[FindsBy(How = How.Id, Using = "TextField1")]
private IWebElement FirstTextField{ get; set; }

我已将我的 WebDriverWait 声明如下:

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

我在 HomePage.cs 中有一个名为 Validate() 的方法,我想在其中使用 WebDriverWait如下:

wait.Until(ExpectedConditions.ElementIsVisible(FirstTextField))

我怎样才能以这种方式使用 WebDriverWait,而不必使用 By 定位器:

wait.Until(ExpectedConditions.ElementIsVisible(By.Id("TextField1")));

【问题讨论】:

    标签: c# selenium webdriverwait


    【解决方案1】:

    ExpectedConditions.ElementIsVisible() 在 C# ExpectedConditions 中没有 IWebElement 的重载。您可以使用自定义实现

    public static Func<IWebDriver, IWebElement> ElementIsVisible(IWebElement element)
    {
        return (driver) =>
            {
                try
                {
                    return return element.Displayed ? element : null;
                }
                catch (StaleElementReferenceException)
                {
                    return null;
                }
            };
    }
    

    用途:

    wait.Until(ElementIsVisible(FirstTextField));
    

    【讨论】:

    • 谢谢家伙 - 我在 HomePage 类中有代码,到目前为止没有构建错误。假设我想将它放在一个名为 Element_Extensions 的单独类中,该类位于 Extensions 文件夹中,我将如何从我的 HomePage 类中调用它。我已经声明使用projName.Extensions;
    • 我已经对其进行了排序...我将您上面提供的代码复制到了我的 Element_Extensions 类中,然后使用了 Element_Extensions.ElementIsVisible(FirstTextField); ,运行了我的测试并通过了!
    • @SGold 很高兴听到。
    • 道歉我的意思是wait.Until(Element_Extensions.ElementIsVisible(FirstTextField));
    • 您可能还想捕捉NoSuchElementExceptions。有时它没有渲染,所以你无法确定它是否可见。
    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 2022-11-09
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2022-01-24
    相关资源
    最近更新 更多