Check if element is clickable in Selenium Java

2019-01-20 08:54发布

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)

Need a help on this.

5条回答
老娘就宠你
2楼-- · 2019-01-20 09:34

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

ExpectedConditions.elementToBeClickable returns WebElement if expected condition is true otherwise it will throw TimeoutException, It never returns 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();

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.

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..:)

查看更多
ら.Afraid
3楼-- · 2019-01-20 09:40

wait.until(ExpectedConditions) won't return null, it will either meet the condition or 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();
}
查看更多
可以哭但决不认输i
4楼-- · 2019-01-20 09:43

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.

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;
			}
		};
	}

查看更多
贼婆χ
5楼-- · 2019-01-20 09:47
 begin
      @locator.click
      @locator.click
      return true
rescue
      return false
end
查看更多
聊天终结者
6楼-- · 2019-01-20 09:51

There are instances when element.isDisplayed() && element.isEnabled() will return true but still element will not be clickable, because it is hidden/overlapped by some other element. In such case, Exception caught is:

org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (781, 704). Other element would receive the click: ... USE>>>>>>>WebElement element=driver.findElement(By.xpath""); JavascriptExecutor ex=(JavascriptExecutor)driver; ex.executeScript("arguments[0].click()", element);

It will work.

查看更多
登录 后发表回答