检查元素在 Selenium Java 中是否可点击 [英] Check if element is clickable in Selenium Java

查看:39
本文介绍了检查元素在 Selenium Java 中是否可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Selenium 的新手,需要检查 Selenium Java 中的元素是否可点击,因为 element.click() 同时传递 linklabel.

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

我尝试使用下面的代码但不起作用:

I tried using below code but not working:

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)

推荐答案

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

elementToBeClickable is used for checking an element is visible and enabled such that you can click it.

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

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

So if your using ExpectedConditions.elementToBeClickable to find an element which will always gives you the clickable element, so no need to check for null condition, you should try as below :-

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();

正如你所说的 element.click() 同时传递 linklabel 这并不意味着元素不可点击,它表示返回的元素 clicked 但可能没有通过单击操作对元素执行任何事件.

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 but may be there is no event performs on element by click action.

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

Note:- I'm suggesting you always try first to find elements by id, name, className and other locator. if you faced some difficulty to find then use cssSelector and always give last priority to xpath locator because it is slower than other locator to locate an element.

希望对你有帮助..:)

Hope it helps you..:)

这篇关于检查元素在 Selenium Java 中是否可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆