Webdriver如何等到元素在webdriver C#中可点击 [英] Webdriver How to wait until the element is clickable in webdriver C#

查看:19
本文介绍了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# 中):

  1. 等到元素从 DOM 返回
  2. 等到元素的 .Displayed 属性为真(这实际上是 visibilityOfElementLocated 正在检查的内容).
  3. 等到元素的 .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#):

  1. Wait until the element is returned from the DOM
  2. Wait until the element's .Displayed property is true (which is essentially what visibilityOfElementLocated is checking for).
  3. Wait until the element's .Enabled property is true (which is essentially what the elementToBeClickable 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆