2013-04-17 96 views
8

有一個塊Ui在瀏覽器中生成元素後幾秒鐘內覆蓋所有元素,因爲我面臨一個問題,因爲元素已經到來網絡驅動程序嘗試點擊該元素,但點擊被Block UI接收。 我曾嘗試使用等到但我沒有幫助,因爲我可以在C#中找到isClickAble webdriver的Webdriver如何等待,直到該元素可在webdriver中點擊C#

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,在此先感謝

回答

23

那麼考慮看看進入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屬性爲真(這實質上是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代碼所做的直接翻譯G。

+1

感謝ü上述問題我提到的是由於有塊UI,我解決了這個問題,您的解決方案幫我解決一個不同的問題再次感謝u.Your的解決方案是正確的解決方案,它代表了isClickAble正確 –

+6

此解決方案不會確保該元素是* Clickable *。元素可以是'Displayed'和'Enabled',但是*不可點擊*由於**其他元素會收到點擊**。 –

+2

@AlexOkrushko,展開你的意思,給出可重複的場景,例子,完整的錯誤等等。我希望在檢查'.Displayed'和'.Enabled'這兩個錯誤消息,但它不是一件確定的事情,那裏很可能是邊緣案例 - 因此,我會請你提出一個實際情況,情況並非如此。這應該然後提交作爲一個問題的硒開發的。 – Arran

1

下面是我用來檢查它是否可點擊的代碼,否則轉到另一個URL。

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

如果你有一個問題,比如「另一個元素將收到點擊」,解決的辦法是使用一個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")); 
} 
相關問題