即使默认情况下禁用该按钮,Button.isEnabled()也会返回true

时间:2017-06-15 09:09:12

标签: java selenium-webdriver isenabled

在测试时,我遇到了一个路障,我在WebPage中有一个按钮,默认情况下是禁用的。我正在使用Selenium WebDriver来测试默认情况下是否禁用了按钮,boolean总是返回true。

Boolean buttonStatus = (button XPath).isEnabled

如果有人可以帮助我会很棒

Test Button is disabled by default

HTML信息:

<div class="commandbutton commandbutton--theme-disabled commandbutton--recommended">
<button class="commandbutton-button commandbutton-button--disabled" type="button" tabindex="-1">

3 个答案:

答案 0 :(得分:2)

From isEnabled docs

This will generally return true for everything but disabled input elements.

But it will work on buttons as well. However, isEnabled() checks for the disabled attribute. If the button is disabled by JavaScript or any other means isEnabled() won't detect it.

My guess is the button has other classes when it is enabled or disabled. For example, when enabled it probably won't have commandbutton-button--disabled class. You can check for it

WebElement button = driver.findElement(By.xpath("button XPath"));
String classes = button.getAttribute("class");
boolean isDisabled = classes.contains("commandbutton-button--disabled");

答案 1 :(得分:0)

isEnabled can only tell you the button works fine, you need to check the class attribute to check is the button is enabled.

答案 2 :(得分:0)

我遇到了同样的问题。但我在页面上的元素非常奇怪。其中一些硒可以点击,虽然它们不可点击,其中一些硒无法点击,但可以发送密钥给他们。经过几个小时的思考,我编写了通用方法,检查元素是否启用。

在与程序员交谈之后,我知道,他们在这个页面上使用了一些特殊的Select,它看起来像Div with Input。他说,我可以通过检查属性Class of Div来检查它是否禁用。如果'select2-container-disabled'则禁用此输入。

我改变了我的方法。现在它看起来像是:

 public boolean isNotClickable(WebElement... elements) {
    List<WebElement> elementsChecked = new ArrayList<>();
    List<WebElement> elementsToCheckByClass = new ArrayList<>();
    List<WebElement> elementsToCheckByClick = new ArrayList<>();
    List<WebElement> elementsToCheckBySendKeys = new ArrayList<>();

    for (WebElement checkedElement : elements) {
        log.info("Checking, that element [" + getLocator(checkedElement) + "] is not clickable by isEnabled()");
        if (checkedElement.isEnabled()) {
            elementsToCheckByClass.add(checkedElement);
        } else {
            elementsChecked.add(checkedElement);
        }
    }
    if (!elementsToCheckByClass.isEmpty()) {
        for (WebElement checkedByClassElement : elementsToCheckByClass) {
            log.info("Checking, that element [" + getLocator(checkedByClassElement) + "] is not clickable by class");
            String classOfElement = checkedByClassElement.getAttribute("class");
            List<String> classes = new ArrayList<>(Arrays.asList(classOfElement.split(" ")));
            if (!classes.contains("select2-container-disabled")) {
                elementsToCheckByClick.add(checkedByClassElement);
            } else {
                elementsChecked.add(checkedByClassElement);
            }
        }
    }
    if (!elementsToCheckByClick.isEmpty()) {
        WebDriverWait wait = new WebDriverWait(driverUtils.getDriver(), 1);
        for (WebElement checkedByClickElement : elementsToCheckByClick) {
            log.info("Checking, that element [" + getLocator(checkedByClickElement) + "] is not clickable by clicking it");
            try {
                wait.until(elementToBeClickable(checkedByClickElement));
                elementsToCheckBySendKeys.add(checkedByClickElement);
            } catch (Exception e) {
                elementsChecked.add(checkedByClickElement);
            }
        }
    }
    if (!elementsToCheckBySendKeys.isEmpty()) {
        for (WebElement checkedBySendKeysElement : elementsToCheckBySendKeys) {
            log.info("Checking, that element [" + getLocator(checkedBySendKeysElement) + "] is not clickable by sending keys");
            try {
                checkedBySendKeysElement.sendKeys("checking");
                return false;
            } catch (Exception e) {
                elementsChecked.add(checkedBySendKeysElement);
            }
        }
    }
    return elementsChecked.size() == elements.length;
}