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

SKV picture SKV · Jun 15, 2017 · Viewed 14.9k times · Source

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">

Answer

Guy picture Guy · Jun 15, 2017

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