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

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

var example = _wait.Until((d) => d.FindElement(By.XPath("Example"))); var example2 = _wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("Example"))); example.click(); example2.click(); 

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

好好看看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属性为true(这实际上是visibilityOfElementLocated正在检查的内容)。
  3. 等到元素的.Enabled属性为true(这实际上是elementToBeClickable正在检查的内容)。

我会像这样实现它(添加到当前的ExpectedConditions集合,但有多种方法可以做到:

 ///  /// An expectation for checking whether an element is visible. ///  /// The locator used to find the element. /// The  once it is located, visible and clickable. public static Func ElementIsClickable(By locator) { return driver => { var element = driver.FindElement(locator); return (element != null && element.Displayed && element.Enabled) ? element : null; }; } 

适用于:

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

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

这是我用来检查它是否可点击的代码,否则转到另一个URL。

 if (logOutLink.Exists() && ExpectedConditions.ElementToBeClickable(logOutLink).Equals(true)) { logOutLink.Click(); } else { Browser.Goto("/"); } 

如果您遇到诸如“另一个元素会收到点击”之类的问题,那么解决这个问题的方法是使用等待覆盖框消失的while循环。

 //The below code waits 2 times in order for the problem element to go away. int attempts = 2; var elementsWeWantGone = WebDriver.FindElements(By.Id("id")); while (attempts > 0 && elementsWeWantGone.Count > 0) { Thread.Sleep(500); elementsWeWantGone = WebDriver.FindElements(By.Id("id")); } 

我正面临这个问题,并检查元素是否可点击且可见是不够的,Selenium仍未等待。

我为我的案例找到的唯一解决方案是一个不好的做法,但作为一种解决方法的function。 我尝试使用Try / Catch将循环元素放入循环中,如Falgun Cont响应中所示:

StackExchange – 如何使用C#等待WebDriver中的元素可单击