【问题标题】:Is there a way to use the FindsBy annotation for WebDriverWait in the C# port of Selenium?有没有办法在 Selenium 的 C# 端口中为 WebDriverWait 使用 FindsBy 注释?
【发布时间】:2013-11-03 18:32:43
【问题描述】:

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

[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)")));

有什么想法吗?

【问题讨论】:

  • 我不使用 C#,但我知道许多 ExpectedConditions 只接受 By 而不是 WebElement。但是,在 Java 中,等到元素可见(不存在,可见)是您可以使用 WebElement 完成的操作
  • 我试过 FindsBy 只是在每个网络元素上都有错误。 FindElement(By.Id/Xpath 工作。Examples of PageFactory in java。您的 PageFactory 初始化元素行在哪里?需要更多关于似乎不起作用的详细信息;异常、构建失败、测试失败。什么样的 wait
  • 您不能只使用 By 对象吗? public By FirstLocationTile => By.CssSelector("div.location:nth-child(1) > div:nth-child(3)"); wait.Until(ExpectedConditions.ElementIsVisible(FirstLocationTile));

标签: c# selenium


【解决方案1】:

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

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

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

An example lambda expression use can be found in the C# documentation,下面是您想要实现的示例:

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

我使用 Displayed 和 Enabled 作为示例,因为元素 in the C# documentation 没有 Visible 属性。

【讨论】:

    【解决方案2】:

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

        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));
    

    【讨论】:

      猜你喜欢
      • 2012-11-04
      • 2021-07-21
      • 2016-10-29
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      相关资源
      最近更新 更多