30

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 the following code, but it is 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)
ouflak
  • 2,458
  • 10
  • 44
  • 49
Sandeep Krishnappa
  • 313
  • 2
  • 4
  • 7

7 Answers7

36

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

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • So should I use listeners? to check any event on the label – Sandeep Krishnappa Jul 12 '16 at 13:01
  • @SandeepKrishnappa No need, You should try to perform click first on browser console at label using javascript..and verify it receives any click event or not...if receives then definitely it will click by selenium.. – Saurabh Gaur Jul 12 '16 at 13:16
  • @SaurabhGaur - I have a similar question. I was wondering if you could answer it for me? thank you. https://stackoverflow.com/questions/60762906/selenium-is-it-okay-to-mix-implicit-wait-and-explicit-wait-like-this – armani Mar 19 '20 at 18:19
26

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

rene
  • 41,474
  • 78
  • 114
  • 152
user8639449
  • 261
  • 3
  • 2
  • 2
    This is exactly true, most of the people are not aware of this fact! – Rajagopalan Aug 02 '18 at 17:04
  • @Rajagopalan : Exactly , your element should be visible in UI and there should not be any overlay. – cruisepandey Aug 02 '18 at 17:18
  • @cruisepandey visible and enable is not enough to know whether element is clickable, that's why elementToBeClickable fails in our last question we discussed, here this user explains the same in his answer. – Rajagopalan Aug 02 '18 at 17:22
  • @cruisepandey Read his answer more clearly he explains that `element.isDisplayed() && element.isEnabled() will return true but still element will not be clickable` – Rajagopalan Aug 02 '18 at 17:25
  • I'm trying to recreate this with another library, but I'm not sure I understand how it works. Can you explain your solution a little? – dǝɥɔS ʇoıןןƎ May 22 '19 at 09:51
  • 3
    This is true and probably works in most cases but it is not the proper way of handling the problems. First - click by javascript is not the same as click by mouse - there might be some differences causing unreliable test results. Second - if the element is overlapped and not clickable - it means user cannot click it either - therefor it is correct when the test fails with an exception, as the user could not do the action either. – Lukas K Feb 14 '20 at 14:59
10

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();
}
Guy
  • 46,488
  • 10
  • 44
  • 88
  • 14
    There are instances when `element.isDisplayed() && element.isEnabled()` will return `true` but still element will not be clickable, because Element 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: ` – Om Sao Aug 30 '17 at 13:56
2

There are certain things you have to take care:

  • WebDriverWait inconjunction with ExpectedConditions as elementToBeClickable() returns the WebElement once it is located and clickable i.e. visible and enabled.
  • In this process, WebDriverWait will ignore instances of NotFoundException that are encountered by default in the until condition.
  • Once the duration of the wait expires on the desired element not being located and 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(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 located and clickable, wrap up the WebDriverWait in a try-catch{} block as follows:

      try {
             new WebDriverWait(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 isn't clickable");
          }
      
    • If WebDriverWait returns the located and clickable element but the element is still not clickable, you need to invoke executeScript() method as follows:

      WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))); 
      ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • DebanjanB - I have a similar question here. Could you please answer it for me? Thank you. https://stackoverflow.com/questions/60762906/selenium-is-it-okay-to-mix-implicit-wait-and-explicit-wait-like-this – armani Mar 19 '20 at 18:19
1

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;
            }
        };
    }
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Leo Zhao
  • 19
  • 1
1

the class attribute contains disabled when the element is not clickable.

WebElement webElement = driver.findElement(By.id("elementId"));
if(!webElement.getAttribute("class").contains("disabled")){
    webElement.click();
}
F. Müller
  • 3,969
  • 8
  • 38
  • 49
afaj1c
  • 19
  • 2
0
List<WebElement> wb=driver.findElements(By.xpath(newXpath));
        for(WebElement we: wb){
            if(we.isDisplayed() && we.isEnabled())
            {
                we.click();
                break;
            }
        }
    }