How to check if the button is clickable using selenium webdriver

不打扰是莪最后的温柔 提交于 2020-01-17 07:08:33

问题


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')]"}


回答1:


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



回答2:


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')]


来源:https://stackoverflow.com/questions/44917575/how-to-check-if-the-button-is-clickable-using-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!