2014-03-26 21 views
2

在Selenium框架中,ExpectedConditions.elementIsClickable暗示該元素也是「可見,啓用和呈現」。 ExpectedConditions.presenceOfElementLocatedBy似乎只是一個簡單的findElement調用。Selenium中的「可點擊」元素是否意味着它是「可見的和現在的」?

通過查看源代碼,這裏是我幹嘛這麼遠約ExpectedConditions行爲,從方法:

1. findElement() = can throw NoSuchElementException, or WebDriverException 
2. elementToBeClickable() = is visible, !null, and enabled, catches 
     a StaleElementReferenceException 
3. visbilityOf() = displayed, catches no exception 
4. presenceOfElementLocated() = simple findElement call, catches findElement() 
     exceptions 
5. stalenessOf() = true if findElement succeeds, can catch StaleElementReferenceException, 
     or findElement() exceptions 
6. visibilityOfElementLocated() = same as stalenessOf, can catch 
     StaleElementReferenceException or the findElement() exceptions 

回答

2

是的,一個可點擊的元素需要元素是可見的。如果元素被隱藏,則Click()將失敗並拋出ElementNotVisibleException

例如:對於下面隱藏的span,Click()將拋出一個ElementNotVisibleException與消息Element is not currently visible and so may not be interacted with

var driver = new FirefoxDriver(); 
driver.Navigate().GoToUrl("data:text/html,<span id=\"hello\" style=\"display:none;\">Hello</span>"); 
driver.FindElement(By.Id("hello")).Click(); 
+0

謝謝!謝謝!謝謝!你只是給了我一種方法來爲我的方法編寫單元測試,而不需要外部網站!我非常感謝你!現在我可以編寫單元測試來爲自己證明這些事情。很高興。 – djangofan

+1

:)雖然,我不認爲_clickable_暗示_enabled_,因爲它也可以點擊一個禁用的元素 - 它只是不會做任何事情。 – Faiz

+0

我實際上在WebDriver規範中找到了更詳細的解釋。因此,通過檢查源代碼和規範,我至少能夠對發生的事情有一個模糊的概念。 – djangofan

0

可點擊表示該元素是可見和啓用。另一個單詞元素是Displayed和Enabled。
如果有任何疑問,您隨時可以檢查ExpectedConditions執行情況。

相關問題