About this lesson
Check if an element is disabled in Selenium Java: isEnabled() and the disabled attribute
Learn Selenium JavaIntermediateCheckboxes & Radio Buttons
How it works
There are two ways to ask whether a form control is disabled. isEnabled() returns false for a disabled control, and getAttribute("disabled") returns "true" when the attribute is present and null when it is absent. In the HTML of the Checkboxes page the attribute is written as disabled="", and Selenium still reports it as "true".
A disabled checkbox does not complain when clicked. Clicking firstSelect3 throws nothing; the box simply stays unticked. That silence is why tests check the state up front instead of relying on an exception.
isEnabled() also respects disabled containers. An input inside a <fieldset disabled> reports false even though the input itself has no disabled attribute, so isEnabled() is the safer check when you only need a yes or no.
About the assignment
The starter gives you two boxes from the Checkboxes page: a normal one and one from the disabled row. You report whether each is enabled, then confirm the reason for the disabled one by reading its disabled attribute.
What you will do
- Print
isEnabled()for the normal box with the labelNormal checkbox enabled:. - Print
isEnabled()for the disabled box with the labelDisabled checkbox enabled:. - Read the disabled box's
disabledattribute withgetAttribute()and print it with the labelHas disabled attr:.
Common mistakes
Comparing the attribute with "false"
An enabled element returns
null, not "false". Callingequals()on that result throwsNullPointerException.Treating readonly as disabled
A read-only field is still enabled. On Text Fields,
isEnabled()returnstruefor theuneditablefield even though it cannot be typed into.Expecting aria-disabled to count
isEnabled()only knows the realdisabledstate. A control marked witharia-disabled="true"still returnstrue, so read that attribute directly on custom widgets.
In a real test suite
Disabled states guard actions: a Submit button stays disabled until required fields are filled, and a Next button until terms are accepted. Tests assert the disabled state first, fill the form, then assert that the button became enabled.
Lesson 24 handled radio groups. Next, lesson 26 starts the dropdown lessons with the Select class.
Related lessons
- isEnabled() - Check Enabled State
isEnabled()on text fields, including the read-only case. - Get Attribute Value with getAttribute()How boolean attributes come back from
getAttribute(). - Click a ButtonClicking a disabled button is ignored without an exception too.
To try one of these calls on any page without a task, open the Online Selenium Java Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/Checkboxes");
WebElement normal = driver.findElement(By.id("firstSelect1"));
WebElement disabled = driver.findElement(By.id("firstSelect3"));
// TODO: Check if each is enabled
// TODO: Check if disabled checkbox has the "disabled" attribute
driver.quit();
}
}