I’m new to Selenium
and need to check if element is clickable in Selenium
Java
, since element.click()
passes both on link
和 label
。
我尝试使用以下代码,但它不起作用:
WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);
if(wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")))==null)
原文由 Sandeep Krishnappa 发布,翻译遵循 CC BY-SA 4.0 许可协议
elementToBeClickable
用于检查元素是否可见并启用,以便您可以单击它。ExpectedConditions.elementToBeClickable
返回WebElement
如果预期条件为真,否则它会抛出TimeoutException
,它永远不会返回null
.因此,如果您使用
ExpectedConditions.elementToBeClickable
找到一个 始终为您提供可点击元素的元素,那么无需检查null
条件,您应该尝试如下:-As you are saying
element.click()
passes both onlink
andlabel
that’s doesn’t mean element is not clickable, it means returned elementclicked
但可能没有事件通过单击操作对元素执行。注意:- 我建议您总是首先尝试通过
id
、name
、className
和其他定位器来查找元素。如果您在查找时遇到一些困难,请使用cssSelector
并始终将xpath
定位器放在最后,因为它比其他定位器定位元素慢。希望对你有帮助..:)