Skip to content
CodeKruPro

Handle Disabled Form Elements

25/70

Handle Disabled Form Elements

Disabled elements cannot be interacted with. Use isEnabled() to check before interacting.

On the Checkboxes page, disabled checkboxes have IDs like firstSelect3.

Task:

  1. Navigate to https://testkru.com/Elements/Checkboxes
  2. Check if firstSelect1 (normal) is enabled
  3. Check if firstSelect3 (disabled) is enabled
  4. Print the disabled attribute of the disabled checkbox

Expected output:

Normal checkbox enabled: true
Disabled checkbox enabled: false
Has disabled attr: true

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

  1. Print isEnabled() for the normal box with the label Normal checkbox enabled:.
  2. Print isEnabled() for the disabled box with the label Disabled checkbox enabled:.
  3. Read the disabled box's disabled attribute with getAttribute() and print it with the label Has disabled attr:.

Common mistakes

  • Comparing the attribute with "false"

    An enabled element returns null, not "false". Calling equals() on that result throws NullPointerException.

  • Treating readonly as disabled

    A read-only field is still enabled. On Text Fields, isEnabled() returns true for the uneditable field even though it cannot be typed into.

  • Expecting aria-disabled to count

    isEnabled() only knows the real disabled state. A control marked with aria-disabled="true" still returns true, 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.

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.

Main.java
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();
    }
}