Careeroza
Element State Checks
isDisplayed, isEnabled, isSelected
<input type="checkbox" id="remember-me" checked>
<button id="checkout" disabled>Checkout</button>
<p class="error-message" style="display:none">Invalid email or password</p>System.out.println(driver.findElement(By.id("remember-me")).isSelected()); // checkbox / radio ticked?
System.out.println(driver.findElement(By.id("checkout")).isEnabled()); // can it be clicked?
System.out.println(driver.findElement(By.cssSelector(".error-message")).isDisplayed()); // visible?Example
Output (empty cart, page just loaded):
Run StateTest.main()Example output
true false false
Present vs Displayed
| Situation | findElements(...).size() | isDisplayed() |
|---|---|---|
| element in HTML and visible | 1 | true |
| element in HTML but hidden (display:none) | 1 | false |
| element not in HTML | 0 | cannot call: findElement throws NoSuchElementException |
Tip: To check "the error is NOT shown", first check presence with findElements, then isDisplayed() only if it is present.