Java中是否可以单击元素

Java中是否可以单击元素

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

问题描述

我是 Selenium 的新用户,需要检查中的元素是否可点击> Selenium Java ,因为 element.click()同时传递链接标签

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 is used for checking an element is visible and enabled such that you can click it.

ExpectedConditions.elementToBeClickable 返回如果预期条件为真,否则它将抛出,它永远不会返回 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()传递链接标签,这并不意味着元素不可点击,这意味着返回元素点击但可能是因为点击操作没有事件执行。

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.

注意: - 我建议你总是首先尝试按 id name className 和其他定位器。如果你遇到一些难以找到然后使用 cssSelector 并始终给 xpath locator赋予最后一个优先权,因为它比其他的慢找到元素的定位器。

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中是否可以单击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:03