繁体   English   中英

有没有办法在 Selenium 的 C# 端口中为 WebDriverWait 使用 FindsBy 注释?

[英]Is there a way to use the FindsBy annotation for WebDriverWait in the C# port of Selenium?

所以我开始使用这个很棒的功能:

[FindsBy(How = How.CssSelector, Using = "div.location:nth-child(1) > div:nth-child(3)")]
public IWebElement FirstLocationTile { get; set; }

但问题是它在我的 WebDriverWait 代码中似乎不起作用!

具体示例,我无法重复使用我的 FirstLocationTile。 它坚持有一个 By.:

 var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(BaseTest.defaultSeleniumWait));
 wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("div.location:nth-child(1) > div:nth-child(3)")));

有任何想法吗?

如果使用 ExpectedConditions,则只能通过 locator 进行标识,因为 ExpectedConditions 仅接受 locator 作为参数。

但是,ExpectedConditions 并不是您可以在 wait.until() 中使用的唯一参数。 您可以在 lambda 表达式中使用您的元素。

^ 这适用于 C#、Python 和其他语言。

可以在 C# 文档中找到使用 lambda 表达式的示例,下面是您尝试实现的示例:

wait.Until(FirstLocationTile => FirstLocationTile.Displayed && FirstLocationTile.Enabled);

我使用 Displayed 和 Enabled 作为示例,因为C# 文档中没有元素的 Visible 属性。

您可以创建自己的等待方法。 下面的例子:

    public static Func<IWebDriver, bool> ElementIsVisible(IWebElement element)
{
    return (driver) =>
    {
        try
        {
            return element.Displayed;
        }
        catch (Exception)
        {
            // If element is null, stale or if it cannot be located
            return false;
        }
    };
}



public static Func<IWebDriver, IWebElement> ElementIsClickable(IWebElement element)
{
    return driver =>
    {
        return (element != null && element.Displayed && element.Enabled) ? element : null;
    };
}

这些将类似于您的标准等待。

 WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
 wait.Until(ElementIsClickable(FirstLocationTile));

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM