javaseleniumselenium-webdriverisenabled

Button.isEnabled() returns true even though the button is disabled by default


While testing I got a road block where I have a button in a WebPage which is disabled by default. I am using Selenium WebDriver to test if the button is disabled by default the boolean is always returning true.

Boolean buttonStatus = (button XPath).isEnabled

It will be great if someone can help me

Test Button is disabled by default

HTML Information:

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

Solution

  • 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");