问题描述
在浏览器中生成元素后,有一个块 Ui 覆盖了所有元素几秒钟,因此我面临一个问题,由于元素已经存在,网络驱动程序尝试单击该元素,但是Block UI 接收到点击.我曾尝试使用等待直到但我没有帮助,因为我可以在 C# webdriver 中找到 isClickAble
var example = _wait.Until((d) => d.FindElement(By.XPath("Example")));var example2 = _wait.Until<IWebElement>(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# 中):
- 等到元素从 DOM 返回
- 等到元素的
.Displayed
属性为真(这实际上是visibilityOfElementLocated
正在检查的内容). - 等到元素的
.Enabled
属性为真(这实际上是elementToBeClickable
正在检查的内容).
我会这样实现(添加到当前的 ExpectedConditions
集,但是有多种方法可以做到:
///<总结>///检查元素是否可见的期望.///</总结>///<param name="locator">用于查找元素的定位器.</param>///<returns><see cref="IWebElement"/>一旦它被定位,可见和可点击.</returns>公共静态函数<IWebDriver,IWebElement>ElementIsClickable(按定位器){返回驱动程序=>{var element = driver.FindElement(locator);返回(元素!= null && element.Displayed && element.Enabled)?元素:空;};}
可用于:
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Id("id")));
但是,您可能对 clickable 的含义有不同的理解,在这种情况下,此解决方案可能不起作用 - 但它是对 Java 代码所做工作的直接翻译.p>
There is a block Ui which covers all the elements for a few seconds after the Element have been generated in the browser because of this i facing a problem ,Since element has come into existence the web-driver try to click the element but the click is received by Block UI . I have tried to use the wait Until but i did not help ,Since i can find isClickAble in C# webdriver
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();
Is there C# equivalent for isClickAble ,Thanks in advance
Well taking a look into the Java source, tells me it is basically doing two things to determine if it's 'clickable':
https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java
Firstly, it'll check if it's 'visible' by using the standard ExpectedConditions.visibilityOfElementLocated
, it'll then simply check if the element.isEnabled()
is true
or not.
This can be condensed slightly, this basically means (simplified, in C#):
- Wait until the element is returned from the DOM
- Wait until the element's
.Displayed
property is true (which is essentially whatvisibilityOfElementLocated
is checking for). - Wait until the element's
.Enabled
property is true (which is essentially what theelementToBeClickable
is checking for).
I would implement this like so (adding onto the current set of ExpectedConditions
, but there are multiple ways of doing it:
/// <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;
};
}
Usable in something like:
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Id("id")));
However, you might have a different idea of what clickable might mean, in which case, this solution may not work - but it is a direct translation of what the Java code is doing.
这篇关于Webdriver如何等到元素在webdriver C#中可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!