gpt4 book ai didi

c# - Webdriver 如何等到元素在 webdriver C# 中可点击

转载 作者:可可西里 更新时间:2023-11-01 03:13:47 24 4
gpt4 key购买 nike

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

   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();

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 集,但有多种方法可以实现:

/// <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;
};
}

可用于:

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

但是,您可能对 clickable 可能意味着什么有不同的想法,在这种情况下,此解决方案可能不起作用 - 但它是 Java 代码正在执行的操作的直接转换。

关于c# - Webdriver 如何等到元素在 webdriver C# 中可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16057031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com