Skip to content
CodeKruPro

isEnabled() - Check Enabled State

33/70

isEnabled() - Check Enabled State

isEnabled() returns true if an element can accept user input. Disabled elements return false.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Check if firstName (enabled) is enabled
  3. Check if disabledField (disabled) is enabled

Expected output:

firstName enabled: true
disabledField enabled: false
Read full tutorial on Codekru

About this lesson

isEnabled in Selenium Java: check whether a field accepts input

Selenium Java AssignmentsIntermediateElement States

How it works

isEnabled() returns false only when a form control is disabled, either by its own disabled attribute or by a disabled fieldset around it. For everything else it returns true, including a div or a paragraph that can never be "used" at all.

Enabled does not mean editable or visible. On Text Fields, the read-only uneditable field and the hidden invisibleField both return true. Only disabledField, which has the disabled attribute, returns false.

Each state check answers one question. isEnabled() says whether the control is disabled, isDisplayed() whether it is visible, and the readonly attribute whether it can be edited. Tests that need to know whether a user can type need all three.

About the assignment

Text Fields has an ordinary first-name input and a disabled input that still shows a value. You call isEnabled() on each and print the results, so you see which state the disabled attribute changes.

What you will do

  1. Find the firstName field and the disabledField field by id.
  2. Call isEnabled() on each.
  3. Print one line per field in the order and format the task shows.

Common mistakes

  • Assuming disabled means hidden

    disabledField is displayed, and you can still read its value. It just cannot be typed into.

  • Typing without checking

    sendKeys() on a disabled field throws ElementNotInteractableException. Check isEnabled() first when the state can vary.

  • Reading the value as a sign of state

    disabledField still holds a value you can read with getAttribute("value"), so a non-empty value says nothing about whether the field accepts input. Ask isEnabled().

In a real test suite

Form tests assert that fields lock and unlock at the right time: a shipping address disabled while "same as billing" is ticked, or a Save button disabled until something changes.

Lesson 32 checked visibility. Lesson 34 finishes the state checks with isSelected() on checkboxes.

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

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/TextFields");
        
        // TODO: Check isEnabled() for firstName and disabledField
        
        driver.quit();
    }
}
Read the full tutorial