Webdriver 如何等待元素在 webdriver C# 中可点击 [英] Webdriver How to wait until the element is clickable in webdriver C#
问题描述
在浏览器中生成元素后,有一个块 Ui 会在几秒钟内覆盖所有元素,因此我面临一个问题,由于元素已经存在,网络驱动程序尝试单击该元素,但是点击由 Block UI 接收.我曾尝试使用等待直到但我没有帮助,因为我可以在 C# webdriver 中找到 isClickAble
var example = _wait.Until((d) => d.FindElement(By.XPath("Example")));var example2 = _wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("Example")));例子.click();示例2.click();
是否有等效于 isClickAble 的 C#,提前致谢
看看 Java 源代码,告诉我它基本上做了两件事来确定它是否可点击":
首先,它会使用标准的 ExpectedConditions.visibilityOfElementLocated
检查它是否可见",然后它会简单地检查 element.isEnabled()
是否为true
与否.
这可以稍微浓缩,这基本上意味着(简化,在 C# 中):
- 等待元素从 DOM 返回
- 等到元素的
.Displayed
属性为真(这实际上是visibilityOfElementLocated
正在检查的内容). - 等到元素的
.Enabled
属性为 true(这实际上是elementToBeClickable
正在检查的内容).
我会这样实现(添加到当前的 ExpectedConditions
集,但有多种方法可以做到:
////<摘要>///检查元素是否可见的期望.///</总结>///<param name="locator">用于查找元素的定位器.</param>///<returns>ElementIsClickable(按定位器){返回驱动程序 =>{var element = driver.FindElement(locator);return (element != null && element.Displayed && element.Enabled) ?元素:空;};}
可用于:
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Id("id")));
但是,您可能对 clickable 的含义有不同的看法,在这种情况下,此解决方案可能不起作用 - 但它是对 Java 代码正在执行的操作的直接转换.>
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':
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# 中可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!