检查 Selenium Java 中的元素是否可点击

新手上路,请多包涵

I’m new to Selenium and need to check if element is clickable in Selenium Java , since element.click() passes both on linklabel

我尝试使用以下代码,但它不起作用:

 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 许可协议

阅读 1k
1 个回答

elementToBeClickable 用于检查元素是否可见并启用,以便您可以单击它。

ExpectedConditions.elementToBeClickable 返回 WebElement 如果预期条件为真,否则它会抛出 TimeoutException ,它永远不会返回 null .

因此,如果您使用 ExpectedConditions.elementToBeClickable 找到一个 始终为您提供可点击元素的元素,那么无需检查 null 条件,您应该尝试如下:-

 WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")));
element.click();

As you are saying element.click() passes both on link and label that’s doesn’t mean element is not clickable, it means returned element clicked 但可能没有事件通过单击操作对元素执行。

注意:- 我建议您总是首先尝试通过 idnameclassName 和其他定位器来查找元素。如果您在查找时遇到一些困难,请使用 cssSelector 并始终将 xpath 定位器放在最后,因为它比其他定位器定位元素慢。

希望对你有帮助..:)

原文由 Saurabh Gaur 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题