如何使用Selenium WebDriver检查按钮是否可单击 [英] How to check if the button is clickable using selenium webdriver

查看:903
本文介绍了如何使用Selenium WebDriver检查按钮是否可单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图查找按钮元素是否可单击,但我无法使用Selenium Webdriver进行成功验证.

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;

这是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>

我不断收到以下验证消息: 没有这样的元素:无法找到元素:{"method":"xpath","selector":"//button [contains(.,'Install a new file')]"}

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

推荐答案

只需编写以下方法,并在需要检查元素是否可单击时调用它.还传递必需的参数.

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

这篇关于如何使用Selenium WebDriver检查按钮是否可单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆