如何等待,直到元素是可点击的Webdriver c#

本文关键字:Webdriver 元素 何等待 等待 | 更新日期: 2023-09-27 18:09:41

在浏览器中生成元素后,有一个块Ui覆盖了所有元素几秒钟,因为它面临一个问题,由于元素已经存在,web驱动程序试图点击元素,但点击被块Ui接收。我试过使用等待,但我没有帮助,因为我可以在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();

c#中是否有对应的isClickAble,提前感谢

如何等待,直到元素是可点击的Webdriver 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代码所做的直接翻译。

这是我用来检查它是否可点击的代码,否则转到另一个URL

if (logOutLink.Exists() && ExpectedConditions.ElementToBeClickable(logOutLink).Equals(true))
            {
                logOutLink.Click();
            }
            else
            {
                Browser.Goto("/");
            }

如果你有一个问题,如"另一个元素将接收点击",解决这个问题的方法是使用一个while循环,等待覆盖框消失。

//The below code waits 2 times in order for the problem element to go away.
int attempts = 2;
var elementsWeWantGone = WebDriver.FindElements(By.Id("id"));
while (attempts > 0 && elementsWeWantGone.Count > 0)
{
    Thread.Sleep(500);
    elementsWeWantGone = WebDriver.FindElements(By.Id("id"));
}

在我们较慢的测试运行程序计算机上,似乎可以首先找到输入元素,但在脚本试图向输入控件发送键或单击时仍然无法单击。等待"ElementToBeClickable"有帮助。更快,更强大的测试运行器系统几乎没有这个问题。

下面是一些带有上下文的代码,这些代码似乎改善了我基于c#的Selenium测试。同样使用SpecFlow和xUnit。我们使用IE11和IEDriverServer.exe。

截至2019年5月,包含此内容的NuGet包为DotNetSeleniumExtras.WaitHelpers.

关键行是这一行:

wait.Until (SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(由…

对新页面上的第一个输入字段执行此操作,使用指向您想要交互的第一个输入字段的选择器。(By.XPath (" ")

[Given(@"I have selected the link to the OrgPlus application")]
public void GivenIHaveSelectedTheLinkToTheOrgPlusApplication()
{
    _driver.Navigate().GoToUrl("http://orgplus.myorg.org/ope?footer");
}
[Given(@"I have selected the link to the OrgPlus Directory lookup")]
public void GivenIHaveSelectedTheLinkToTheOrgPlusDirectoryLookup()
{
    var wait = new WebDriverWait(_driver, new TimeSpan(0, 0, 30));
    var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='"lnkDir'"]")));
    IWebElement btnSearch = _driver.FindElement(By.XPath("//*[@id='"lnkDir'"]"));
    btnSearch.SendKeys(Keys.Enter);
}