We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Use isEnabled() and isSelected() to verify if elements are interactive or checked.
In this practice, you'll learn how to check whether an element:
We'll focus on two WebElement methods:
isEnabled() returns true if the element is active and ready for interaction.false if the element is disabled (for example, has the disabled attribute).isEnabled() in your assertions to make sure your UI follows business rules.isEnabled() is false — that usually means the UI is preventing the action on purpose.isSelected() returns true if a checkbox, radio button, or <option> element is currently selected.<option> items in a <select> dropdown.isSelected() on elements that actually have a selectable/checked state.isSelected() is not meaningful.You are testing a simple "Terms and Submit" form with the following elements:
Elements on the page:
id="accept-terms"id="submit-btn"Checkbox state: Not selected
Button state: Disabled
Check the checkbox to enable the Submit button. Use Selenium to verify states with isSelected() and isEnabled()!
!isEnabled()).disabled attribute from the button.isEnabled() returns true).You will automate exactly this flow using isSelected() for the checkbox and isEnabled() for the button.
12345678910111213141516171819WebElement checkbox = driver.findElement(By.id("accept-terms"));
WebElement button = driver.findElement(By.id("submit-btn"));
// 1. Initial State Check
if (!checkbox.isSelected()) {
System.out.println("Checkbox is NOT selected initially.");
}
if (!button.isEnabled()) {
System.out.println("Button is DISABLED initially.");
}
// 2. Perform Action
checkbox.click();
// 3. Verify New State
if (checkbox.isSelected() && button.isEnabled()) {
System.out.println("Success! Checkbox is selected and button is now enabled.");
}id.id.false.false because it's disabled by the UI.true, the flow is working as expected.Always use isEnabled() before clicking important buttons to avoid flaky tests and confusing failures.
isSelected() ➜ checkboxes, radios, <option>isEnabled() ➜ any interactive control (buttons, inputs, links).
In real tests, replace System.out.println(...) with proper assertions like assertTrue(button.isEnabled()) to make your tests self-verifying.