检查元素在 Selenium Java 中是否可点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38327049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if element is clickable in Selenium Java
提问by Sandeep Krishnappa
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
.
我是新来的Selenium
,需要检查是否元素是可以点击的Selenium
Java
,因为element.click()
无论在传球link
和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)
回答by Guy
wait.until(ExpectedConditions)
won't return null, it will either meet the condition or throw TimeoutException
.
wait.until(ExpectedConditions)
不会返回 null,它会满足条件或 throw TimeoutException
。
You can check if the element is displayed and enabled
您可以检查元素是否显示并启用
WebElement element = driver.findElement(By.xpath);
if (element.isDisplayed() && element.isEnabled()) {
element.click();
}
回答by Saurabh Gaur
elementToBeClickable
is used for checking an element is visible and enabled such that you can click it.
elementToBeClickable
用于检查元素是否可见并启用,以便您可以单击它。
ExpectedConditions.elementToBeClickable
returns WebElement
if expected condition is true otherwise it will throw TimeoutException
, It never returns null
.
ExpectedConditions.elementToBeClickable
返回WebElement
如果预期的条件为真,否则会抛出TimeoutException
,它从来没有回报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 :-
因此,如果您使用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
but may be there is no event performs on element by click action.
正如你说的element.click()
通行证上都link
和label
这并不意味着元素是无法点击,就意味着返回元素clicked
,但通过点击动作可能有元件没有事件后执行。
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.
注: -我建议你总是试图最先找到的元素id
,name
,className
和其他定位。如果您在查找时遇到一些困难,请使用cssSelector
并始终将xpath
定位器放在最后优先级,因为它比其他定位器定位元素要慢。
Hope it helps you..:)
希望对你有帮助..:)
回答by Leo Zhao
From the source code you will be able to view that, ExpectedConditions.elementToBeClickable()
, it will judge the element visible and enabled, so you can use isEnabled()
together with isDisplayed()
. Following is the source code.
从源码中可以看到,ExpectedConditions.elementToBeClickable()
, 会判断元素可见和启用,所以可以isEnabled()
和isDisplayed()
. 以下是源代码。
public static ExpectedCondition<WebElement> elementToBeClickable(final WebElement element) {
return new ExpectedCondition() {
public WebElement apply(WebDriver driver) {
WebElement visibleElement = (WebElement) ExpectedConditions.visibilityOf(element).apply(driver);
try {
return visibleElement != null && visibleElement.isEnabled() ? visibleElement : null;
} catch (StaleElementReferenceException arg3) {
return null;
}
}
public String toString() {
return "element to be clickable: " + element;
}
};
}
回答by user8639449
There are instances when element.isDisplayed() && element.isEnabled()
will return true
but still element will notbe clickable, because it is hidden/overlapped by some other element.
在某些情况下element.isDisplayed() && element.isEnabled()
会返回true
但仍然无法点击元素,因为它被其他元素隐藏/重叠。
In such case, Exception
caught is:
在这种情况下,Exception
被捕获的是:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (781, 704). Other element would receive the click:
<div class="footer">...</div>
org.openqa.selenium.WebDriverException:未知错误:元素在点 (781, 704) 处不可点击。其他元素将收到点击:
<div class="footer">...</div>
Use this code instead:
请改用此代码:
WebElement element=driver.findElement(By.xpath"");
JavascriptExecutor ex=(JavascriptExecutor)driver;
ex.executeScript("arguments[0].click()", element);
It will work.
它会起作用。
回答by Abhishek Kumar Singh
List<WebElement> wb=driver.findElements(By.xpath(newXpath));
for(WebElement we: wb){
if(we.isDisplayed() && we.isEnabled())
{
we.click();
break;
}
}
}
回答by DebanjanB
There are certain things you have to take care:
有些事情你必须注意:
- WebDriverWaitinconjunction with ExpectedConditionsas elementToBeClickable()returns the WebElementonce it is locatedand clickablei.e. visibleand enabled.
- In this process, WebDriverWaitwill ignore instances of
NotFoundException
that are encountered by default in theuntil
condition. - Once the duration of the waitexpires on the desired element not being locatedand clickable, will throw a timeout exception.
The different approach to address this issue are:
To invoke
click()
as soon as the element is returned, you can use:new WebDriverWait(Scenario1Test.driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))).click();
To simply validate if the element is locatedand clickable, wrap up the WebDriverWaitin a
try-catch{}
block as follows:try { new WebDriverWait(Scenario1Test.driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))); System.out.println("Element is clickable"); } catch(TimeoutException e) { System.out.println("Element wasn't clickable"); }
If WebDriverWaitreturns the locatedand clickableelement but the element is still not clickable, you need to invoke
executeScript()
method as follows:WebElement element = new WebDriverWait(Scenario1Test.driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))); ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
- WebDriverWait与ExpectedConditions 结合,作为elementToBeClickable()一旦它被定位并且可点击即返回WebElement可见和启用。
- 在此过程中,WebDriverWait将忽略条件中
NotFoundException
默认遇到的实例until
。 - 一旦等待的持续时间在所需元素未被定位和点击时到期,将引发超时异常。
解决这个问题的不同方法是:
要
click()
在元素返回后立即调用,您可以使用:new WebDriverWait(Scenario1Test.driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))).click();
要简单地验证元素是否已定位和可点击,请将WebDriverWait包装在一个
try-catch{}
块中,如下所示:try { new WebDriverWait(Scenario1Test.driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))); System.out.println("Element is clickable"); } catch(TimeoutException e) { System.out.println("Element wasn't clickable"); }
如果WebDriverWait返回了定位且可点击的元素,但该元素仍然不可点击,则需要调用
executeScript()
如下方法:WebElement element = new WebDriverWait(Scenario1Test.driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))); ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);