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
- Find the
firstNamefield and thedisabledFieldfield by id. - Call
isEnabled()on each. - Print one line per field in the order and format the task shows.
Common mistakes
Assuming disabled means hidden
disabledFieldis displayed, and you can still read its value. It just cannot be typed into.Typing without checking
sendKeys()on a disabled field throwsElementNotInteractableException. CheckisEnabled()first when the state can vary.Reading the value as a sign of state
disabledFieldstill holds a value you can read withgetAttribute("value"), so a non-empty value says nothing about whether the field accepts input. AskisEnabled().
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.
Related lessons
- Handle Disabled Form ElementsCheck a disabled checkbox and its
disabledattribute. - Type Text with sendKeys()What happens when you type into a disabled field.
- isDisplayed() - Check Element VisibilityVisibility, the state
isEnabled()does not cover.
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.
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();
}
}