Webdriver如何等待元素在webdriver C#中可单击

Ana*_*d S 15 c# webdriver selenium-webdriver

在浏览器中生成元素后,有一个块Ui覆盖所有元素几秒钟,因为我面临一个问题,因为元素已经存在,web驱动程序尝试单击元素但是单击是Block UI收到.我曾尝试使用wait Until但我没有帮助,因为我可以在C#webdriver中找到isClickAble

   var example = _wait.Until<IWebElement>((d) => d.FindElement(By.XPath("Example")));
   var example2 = _wait.Until<IWebElement>(ExpectedConditions.ElementIsVisible(By.XPath("Example")));
example.click();
example2.click();
Run Code Online (Sandbox Code Playgroud)

isClickAble是否有C#等价物,提前谢谢

Arr*_*ran 32

好好看看Java源码,告诉我它基本上做了两件事来确定它是否是"可点击的":

https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java

首先,它会检查它是否是通过使用标准的"可见的" ExpectedConditions.visibilityOfElementLocated,它会然后简单地检查是否element.isEnabled()true或不是.

这可以稍微压缩,这基本上意味着(简化,在C#中):

  1. 等到元素从DOM返回
  2. 等到元素的.Displayed属性为真(这实际上visibilityOfElementLocated是检查的内容).
  3. 等到元素的.Enabled属性为真(这实际上elementToBeClickable是检查的内容).

我会像这样实现它(添加到当前的一组ExpectedConditions,但有多种方法:

/// <summary>
/// An expectation for checking whether an element is visible.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The <see cref="IWebElement"/> once it is located, visible and clickable.</returns>
public static Func<IWebDriver, IWebElement> ElementIsClickable(By locator)
{
    return driver =>
    {
        var element = driver.FindElement(locator);
        return (element != null && element.Displayed && element.Enabled) ? element : null;
    };
}
Run Code Online (Sandbox Code Playgroud)

可用于:

var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Id("id")));
Run Code Online (Sandbox Code Playgroud)

但是,您可能对可点击的含义有不同的看法,在这种情况下,此解决方案可能无效 - 但它是Java代码正在执行的操作的直接转换.

  • 此解决方案不能确保元素是*Clickable*.元素可以是`显示'和'启用',但*不可点击*由于**其他元素将收到点击**. (7认同)
  • @AlexOkrushko,扩展您的意思,提供可重现的场景,示例,完整的错误等.我希望在检查`.Displayed`和`.Enabled`时会覆盖错误消息,但这不是一个确定的事情,可能边缘情况很好 - 因此我恳请你想出一个实际情况,事实并非如此.然后应将此作为问题提交给Selenium开发人员. (2认同)