簡體   English   中英

Webdriver如何等待元素在webdriver C#中可單擊

[英]Webdriver How to wait until the element is clickable in webdriver C#

在瀏覽器中生成元素后,有一個塊Ui覆蓋所有元素幾秒鍾,因為我面臨一個問題,因為元素已經存在,web驅動程序嘗試單擊元素但是單擊是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屬性為true(這實際上是visibilityOfElementLocated正在檢查的內容)。
  3. 等到元素的.Enabled屬性為true(這實際上是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")));

但是,您可能對可點擊的含義有不同的看法,在這種情況下,此解決方案可能無效 - 但它是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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM