Webdriver How to wait until the element is clickable in webdriver C#(Webdriver如何等到元素在webdriver C#中可点击)
问题描述
在浏览器中生成元素后,有一个块 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
.Displayedproperty is true (which is essentially whatvisibilityOfElementLocatedis checking for). - Wait until the element's
.Enabledproperty is true (which is essentially what theelementToBeClickableis 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#中可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Webdriver如何等到元素在webdriver C#中可点击
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
