Java 如何使用selenium webdriver检查按钮是否可单击

Java 如何使用selenium webdriver检查按钮是否可单击,java,selenium,selenium-webdriver,pageobjects,Java,Selenium,Selenium Webdriver,Pageobjects,我试图找出按钮元素是否可单击,但使用SeleniumWebDriver无法成功验证 下面是验证元素是否可单击的代码 boolean installAFile; String classValues = driver.findElement(by.XPATH("//button[contains(., 'Install a new file')]")).getAttribute("class"); installAFIle = classValues.contains("

我试图找出按钮元素是否可单击,但使用SeleniumWebDriver无法成功验证

下面是验证元素是否可单击的代码

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

只要编写下面的方法,并在需要检查元素是否可单击时调用它即可。同时传递所需的参数

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

只要编写下面的方法,并在需要检查元素是否可单击时调用它。同时传递所需的参数

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;
        }
    }
元素xpath将是; 或

元素xpath将是; 或


您好,请尝试使用selenium等待条件。请查看此链接。这可能有助于xpath,字符串区分大小写。请检查该案例的字符串“Install a new file”。@zen感谢您的建议,我也实现了te wait,但仍然没有luck@SujaiKrishna链接始终是必须可单击的链接。因此你的问题对我来说毫无意义。你能用你想要执行的具体用例和手动步骤来更新这个问题吗?ThanksHi尝试使用selenium等待条件。请查看此链接,这可能对xpath有所帮助,字符串区分大小写。请检查该案例的字符串“Install a new file”。@zen感谢您的建议,我也实现了te wait,但仍然没有luck@SujaiKrishna链接始终是必须可单击的链接。因此你的问题对我来说毫无意义。你能用你想要执行的具体用例和手动步骤来更新这个问题吗?谢谢
//button/div
//div[contains(@class,'iconbutton-button-label')]
//*[contains(text(), 'Install a new file')]