1

I am trying to find if the button element is clickable which I am not able to validate successfully using selenium webdriver.

Here is my code to validate if the element is clickable

    boolean installAFile;

    String classValues = driver.findElement(by.XPATH("//button[contains(., 'Install a new file')]")).getAttribute("class");
    installAFIle = classValues.contains("iconbutton-button--clickable");

    return installAFIle;

Here is the HTML

<div>
<!-- react-text: 406 -->
test message 1
<!-- /react-text -->
<div class="iconbutton">
<button class="iconbutton-button iconbutton-button--clickable" type="button" 
tabindex="0">
<div class="iconbutton-button-label">Install a new file</div>
</button>
</div>
<!-- react-text: 410 -->
under File > Install.
<!-- /react-text -->
</div>

I keep on getting following validation message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(., 'Install a new file')]"}

SKV
  • 133
  • 1
  • 3
  • 14
  • Hi try using selenium wait condition.Please look this link https://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds-in-java[enter link description here](https://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds-in-java) – zen Jul 05 '17 at 05:31
  • This may help https://stackoverflow.com/a/38327476/6743203 – Jay Smith Jul 05 '17 at 05:36
  • In xpath, string is case sensitive. please check the string 'Install a new file' for the case. – Murthi Jul 05 '17 at 05:39
  • @zen thanks for the suggestion, I have implemented te wait as well but still no luck – SKV Jul 05 '17 at 05:43
  • @SujaiKrishna A `link` is always a **_`LINK`_** which must be clickable. Hence your question makes no sense to me. Can you update the Question with your exact usecase and manual steps you are trying to perform? Thanks – undetected Selenium Jul 05 '17 at 06:09
  • @DebanjanB if you see the HTML tag that I add it is to ensure the button is enabled for clicking. so I have to ensure that by default the button is clickable and is not disabled – SKV Jul 05 '17 at 06:21
  • @SujaiKrishna You got an working Answer:) so holding off my Answer. Thanks – undetected Selenium Jul 05 '17 at 06:34
  • @DebanjanB there is no wrong is putting up your solution as well. May be it might help someone in the future. – SKV Jul 05 '17 at 06:39

2 Answers2

6

Just write the below method and call it whenever you want to check whether element is clickable or not. Pass the required arguments also.

public static boolean isClickable(WebElement el, WebDriver driver) 
    {
        try{
            WebDriverWait wait = new WebDriverWait(driver, 6);
            wait.until(ExpectedConditions.elementToBeClickable(el));
            return true;
        }
        catch (Exception e){
            return false;
        }
    }
sForSujit
  • 987
  • 1
  • 10
  • 24
1

Element xpath will be;

/html/body/div/div/button/div

Or

//button/div

Or

//div[contains(@class,'iconbutton-button-label')]

Or

//*[contains(text(), 'Install a new file')]
Aman Kumar
  • 4,533
  • 3
  • 18
  • 40
Dev Perera
  • 21
  • 4