πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 65 of 100
Element Interaction
Beginner

Verifying if an Element is Disabled

🚫Scenario Overview

Verifying if an Element is Disabled

Key Takeaways & Cheat Sheet

  • βœ“Use element.isEnabled() to query functional element states
  • βœ“Check for presence of disabled attribute tags inside components
  • βœ“Inspect class list values for styling states (like "disabled" or "readonly")
  • βœ“Assert returned boolean values using standard assertion frameworks

Short Direct Answer

To check if an element is disabled, use `element.isEnabled()`, which returns false if the element has a `disabled` attribute. For modern custom elements (like styled divs), verify if the element's `class` or `disabled` attributes contain "disabled" or "aria-disabled" values.

⚠️ Senior Warning (Red Flag)

Do not rely only on isEnabled() for custom modern components. Frontend frameworks (like React or Angular) often make divs look disabled using CSS styles while leaving isEnabled() returning true.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

Custom buttons built from divs do not support standard HTML state properties. You must verify their state by inspecting class attributes or accessibility attributes.

SeleniumAutomation.java
Selenium 4 + Java
WebElement saveBtn = driver.findElement(By.id("save-btn"));

// βœ… Standard check: returns false if "disabled" attribute is present
boolean standardCheck = saveBtn.isEnabled(); 

// βœ… Modern check: inspect class properties for disabled state classes
String classes = saveBtn.getAttribute("class");
boolean classStyleCheck = !classes.contains("disabled") && !classes.contains("is-loading");

// βœ… Final Assertion
assert !standardCheck || !classStyleCheck : "Button is active but expected to be disabled";