Popular Searches
Popular Course Categories
Popular Courses

Is Enabled

Working with Web Elements

<div class="selenium-notes">

<h1>isEnabled() in Selenium</h1>

<p><strong>isEnabled()</strong> is a Selenium WebDriver method used to check whether a web element is currently enabled and available for interaction according to WebDriver's enabled-state rules. It returns a boolean value: <strong>true</strong> when the element is enabled and <strong>false</strong> when the element is disabled.</p>

<p>It is commonly used with form controls such as buttons, text fields, checkboxes, radio buttons, select controls, and submit buttons. In Selenium automation, <strong>isEnabled()</strong> is useful for validating whether users can interact with a particular control at a specific point in a test.</p>

<p>JustAcademy's Selenium Automation Testing Course covers Selenium WebDriver, locators, WebElements, TestNG, data-driven testing, Page Object Model, cross-browser testing, automation frameworks, and real-time projects. These concepts provide the foundation for using WebElement methods such as <strong>isEnabled()</strong> in practical automation testing. <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training Course</a></p>

<p>To explore the course through a practical demo session, visit <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Selenium Course Demo</a>.</p>

<hr>

<h2>1. What is isEnabled()?</h2>

<p><strong>isEnabled()</strong> is a method of Selenium's <strong>WebElement</strong> interface. It checks whether the located element is enabled for interaction.</p>

<p>The method returns a boolean value:</p>

<ul>

<li><strong>true</strong> → The element is enabled.</li>

<li><strong>false</strong> → The element is disabled.</li>

</ul>

<p>For example, a Submit button may initially be disabled until the user enters all required information. Selenium can use <strong>isEnabled()</strong> to verify this state.</p>

<hr>

<h2>2. Basic Syntax</h2>

<pre><code>boolean status = element.isEnabled();</code></pre>

<p>Here, <strong>element</strong> represents a Selenium WebElement and <strong>status</strong> stores the enabled state returned by Selenium.</p>

<hr>

<h2>3. Simple Example</h2>

<pre><code>WebElement button = driver.findElement(By.id("submitBtn"));

boolean enabled = button.isEnabled();

System.out.println("Button enabled: " + enabled);</code></pre>

<p>If the button is enabled, the output will be:</p>

<pre><code>Button enabled: true</code></pre>

<p>If the button is disabled, the output will be:</p>

<pre><code>Button enabled: false</code></pre>

<hr>

<h2>4. Why isEnabled() is Important?</h2>

<p>Modern web applications frequently enable or disable controls based on user actions, validation rules, permissions, application state, or data availability.</p>

<p>For example:</p>

<ul>

<li>Login button becomes enabled after valid credentials are entered.</li>

<li>Checkout button becomes enabled after required information is completed.</li>

<li>Submit button becomes enabled after form validation succeeds.</li>

<li>Next button becomes enabled after selecting a required option.</li>

<li>Payment button becomes enabled after accepting terms and conditions.</li>

</ul>

<p>Using <strong>isEnabled()</strong>, automation scripts can verify these application states.</p>

<hr>

<h2>5. isEnabled() Return Type</h2>

<p>The return type of <strong>isEnabled()</strong> is <strong>boolean</strong>.</p>

<pre><code>boolean result = element.isEnabled();</code></pre>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Return Value</th><th>Meaning</th></tr>

<tr><td>true</td><td>The element is enabled.</td></tr>

<tr><td>false</td><td>The element is disabled.</td></tr>

</table>

<hr>

<h2>6. isEnabled() with Button</h2>

<p>One of the most common uses of <strong>isEnabled()</strong> is checking whether a button is enabled.</p>

<pre><code>WebElement loginButton = driver.findElement(By.id("loginBtn"));

if(loginButton.isEnabled()){

    System.out.println("Login button is enabled");

}else{

    System.out.println("Login button is disabled");

}</code></pre>

<hr>

<h2>7. isEnabled() with Text Input</h2>

<p>It can also be used with input fields.</p>

<pre><code>WebElement username = driver.findElement(By.id("username"));

if(username.isEnabled()){

    username.sendKeys("admin");

}</code></pre>

<p>This example enters data only when the username field is enabled.</p>

<hr>

<h2>8. isEnabled() with Password Field</h2>

<pre><code>WebElement password = driver.findElement(By.id("password"));

System.out.println("Password field enabled: " + password.isEnabled());</code></pre>

<hr>

<h2>9. isEnabled() with Checkbox</h2>

<p>A checkbox can also be checked for its enabled state.</p>

<pre><code>WebElement termsCheckbox = driver.findElement(By.id("terms"));

if(termsCheckbox.isEnabled()){

    System.out.println("Terms checkbox can be interacted with");

}</code></pre>

<hr>

<h2>10. isEnabled() with Radio Button</h2>

<pre><code>WebElement maleRadio = driver.findElement(By.id("male"));

if(maleRadio.isEnabled()){

    maleRadio.click();

}</code></pre>

<hr>

<h2>11. isEnabled() with Select Element</h2>

<p>A select control can also be checked before interacting with it.</p>

<pre><code>WebElement country = driver.findElement(By.id("country"));

if(country.isEnabled()){

    Select select = new Select(country);

    select.selectByVisibleText("India");

}</code></pre>

<hr>

<h2>12. HTML disabled Attribute</h2>

<p>HTML form controls can be disabled using the <strong>disabled</strong> attribute.</p>

<pre><code>&lt;button id="submitBtn" disabled&gt;Submit&lt;/button&gt;</code></pre>

<p>For such a button, Selenium can report:</p>

<pre><code>WebElement submit = driver.findElement(By.id("submitBtn"));

System.out.println(submit.isEnabled());</code></pre>

<p>The result is normally:</p>

<pre><code>false</code></pre>

<hr>

<h2>13. Enabled Button Example</h2>

<pre><code>&lt;button id="submitBtn"&gt;Submit&lt;/button&gt;</code></pre>

<pre><code>WebElement submit = driver.findElement(By.id("submitBtn"));

System.out.println(submit.isEnabled());</code></pre>

<p>For an enabled button, Selenium normally returns:</p>

<pre><code>true</code></pre>

<hr>

<h2>14. Disabled Input Example</h2>

<pre><code>&lt;input type="text" id="username" disabled&gt;</code></pre>

<pre><code>WebElement username = driver.findElement(By.id("username"));

System.out.println(username.isEnabled());</code></pre>

<p>The expected result is:</p>

<pre><code>false</code></pre>

<hr>

<h2>15. Enabled Input Example</h2>

<pre><code>&lt;input type="text" id="username"&gt;</code></pre>

<pre><code>WebElement username = driver.findElement(By.id("username"));

System.out.println(username.isEnabled());</code></pre>

<p>The expected result is:</p>

<pre><code>true</code></pre>

<hr>

<h2>16. isEnabled() Does Not Check Visibility</h2>

<p><strong>isEnabled()</strong> and <strong>isDisplayed()</strong> perform different checks. An element can be enabled but not currently displayed.</p>

<pre><code>WebElement element = driver.findElement(By.id("myElement"));

System.out.println("Enabled: " + element.isEnabled());

System.out.println("Displayed: " + element.isDisplayed());</code></pre>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Method</th><th>Purpose</th><th>Return Type</th></tr>

<tr><td>isEnabled()</td><td>Checks enabled state</td><td>boolean</td></tr>

<tr><td>isDisplayed()</td><td>Checks displayed state</td><td>boolean</td></tr>

</table>

<hr>

<h2>17. isEnabled() Does Not Check Selection</h2>

<p><strong>isEnabled()</strong> does not tell whether a checkbox, radio button, or option is selected.</p>

<pre><code>WebElement checkbox = driver.findElement(By.id("subscribe"));

System.out.println("Enabled: " + checkbox.isEnabled());

System.out.println("Selected: " + checkbox.isSelected());</code></pre>

<p>These are two different states.</p>

<ul>

<li><strong>isEnabled()</strong> → Can the control be interacted with?</li>

<li><strong>isSelected()</strong> → Is the control currently selected?</li>

</ul>

<hr>

<h2>18. isEnabled() vs isDisplayed() vs isSelected()</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Method</th><th>Checks</th><th>Return</th><th>Typical Usage</th></tr>

<tr><td>isEnabled()</td><td>Enabled state</td><td>boolean</td><td>Buttons, inputs, controls</td></tr>

<tr><td>isDisplayed()</td><td>Displayed state</td><td>boolean</td><td>Visibility checks</td></tr>

<tr><td>isSelected()</td><td>Selection state</td><td>boolean</td><td>Checkbox, radio, option</td></tr>

</table>

<hr>

<h2>19. Checking Both Displayed and Enabled</h2>

<p>Sometimes a test needs to verify that an element is both visible and enabled.</p>

<pre><code>WebElement loginButton = driver.findElement(By.id("loginBtn"));

if(loginButton.isDisplayed() &amp;&amp; loginButton.isEnabled()){

    System.out.println("Login button is visible and enabled");

}</code></pre>

<hr>

<h2>20. Checking Enabled Before Clicking</h2>

<pre><code>WebElement submitButton = driver.findElement(By.id("submitBtn"));

if(submitButton.isEnabled()){

    submitButton.click();

}else{

    System.out.println("Submit button is disabled");

}</code></pre>

<p>This can be useful when the application intentionally disables a control until some condition is satisfied.</p>

<hr>

<h2>21. isEnabled() Is an Immediate Check</h2>

<p><strong>isEnabled()</strong> checks the element's state at the moment the method is executed. It does not automatically wait for the element to become enabled.</p>

<pre><code>boolean enabled = button.isEnabled();</code></pre>

<p>If the application enables the button two seconds later, the above statement does not wait two seconds and check again.</p>

<hr>

<h2>22. Waiting for an Element to Become Enabled</h2>

<p>For dynamic applications, an explicit wait is usually more appropriate than repeatedly calling <strong>isEnabled()</strong> manually.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement button = wait.until(

    ExpectedConditions.elementToBeClickable(By.id("submitBtn"))

);

button.click();</code></pre>

<p><strong>elementToBeClickable()</strong> is commonly used when the test needs an element that is visible and enabled for clicking.</p>

<hr>

<h2>23. Custom Wait Using isEnabled()</h2>

<p>If the test specifically needs to wait for the enabled state, a custom condition can be used.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(driver -&gt; driver.findElement(By.id("submitBtn")).isEnabled());</code></pre>

<p>For highly dynamic pages, locating the element inside the wait condition can be safer because the DOM may be refreshed or replaced.</p>

<hr>

<h2>24. Login Button Example</h2>

<p>Consider a login page where the Login button is disabled until the required fields are completed.</p>

<pre><code>WebElement username = driver.findElement(By.id("username"));

WebElement password = driver.findElement(By.id("password"));

WebElement loginButton = driver.findElement(By.id("loginBtn"));

username.sendKeys("admin");

password.sendKeys("admin123");

if(loginButton.isEnabled()){

    loginButton.click();

}</code></pre>

<hr>

<h2>25. Form Validation Example</h2>

<p>A form may keep the Submit button disabled until all mandatory fields contain valid information.</p>

<pre><code>WebElement submit = driver.findElement(By.id("submit"));

System.out.println("Before entering data: " + submit.isEnabled());

driver.findElement(By.id("name")).sendKeys("Rahul");

driver.findElement(By.id("email")).sendKeys("[email protected]");

System.out.println("After entering data: " + submit.isEnabled());</code></pre>

<hr>

<h2>26. Terms and Conditions Example</h2>

<p>Some applications enable a Submit or Continue button only after the user accepts terms and conditions.</p>

<pre><code>WebElement terms = driver.findElement(By.id("terms"));

WebElement continueButton = driver.findElement(By.id("continue"));

terms.click();

if(continueButton.isEnabled()){

    continueButton.click();

}</code></pre>

<hr>

<h2>27. E-Commerce Example</h2>

<p>In an e-commerce application, the Checkout button may become enabled only after a valid product or shipping information is available.</p>

<pre><code>WebElement checkout = driver.findElement(By.id("checkout"));

if(checkout.isEnabled()){

    checkout.click();

}else{

    System.out.println("Checkout button is disabled");

}</code></pre>

<hr>

<h2>28. Payment Button Example</h2>

<pre><code>WebElement paymentButton = driver.findElement(By.id("payNow"));

if(paymentButton.isEnabled()){

    System.out.println("Payment button is ready for interaction");

}else{

    System.out.println("Payment button is disabled");

}</code></pre>

<hr>

<h2>29. XPath with isEnabled()</h2>

<p><strong>isEnabled()</strong> can be used with elements located using XPath.</p>

<pre><code>WebElement submit = driver.findElement(

    By.xpath("//button[@type='submit']")

);

System.out.println(submit.isEnabled());</code></pre>

<hr>

<h2>30. CSS Selector with isEnabled()</h2>

<pre><code>WebElement submit = driver.findElement(

    By.cssSelector("button[type='submit']")

);

System.out.println(submit.isEnabled());</code></pre>

<hr>

<h2>31. Checking Multiple Buttons</h2>

<pre><code>List&lt;WebElement&gt; buttons = driver.findElements(By.tagName("button"));

for(WebElement button : buttons){

    System.out.println("Button enabled: " + button.isEnabled());

}</code></pre>

<hr>

<h2>32. Checking Multiple Input Fields</h2>

<pre><code>List&lt;WebElement&gt; inputs = driver.findElements(By.cssSelector("input"));

for(WebElement input : inputs){

    System.out.println(

        input.getAttribute("name") + " enabled: " + input.isEnabled()

    );

}</code></pre>

<hr>

<h2>33. TestNG Assertion with isEnabled()</h2>

<p>In TestNG, <strong>isEnabled()</strong> can be combined with assertions.</p>

<pre><code>WebElement loginButton = driver.findElement(By.id("loginBtn"));

Assert.assertTrue(loginButton.isEnabled());</code></pre>

<hr>

<h2>34. Verify Element Is Disabled</h2>

<pre><code>WebElement submitButton = driver.findElement(By.id("submitBtn"));

Assert.assertFalse(submitButton.isEnabled());</code></pre>

<hr>

<h2>35. Verify Element Becomes Enabled</h2>

<pre><code>WebElement submitButton = driver.findElement(By.id("submitBtn"));

Assert.assertTrue(submitButton.isEnabled());

submitButton.click();</code></pre>

<hr>

<h2>36. isEnabled() and getAttribute("disabled")</h2>

<p>It is possible to inspect the HTML using <strong>getAttribute("disabled")</strong>, but this is not the same as asking WebDriver whether the element is enabled.</p>

<pre><code>WebElement button = driver.findElement(By.id("submitBtn"));

System.out.println("Enabled: " + button.isEnabled());

System.out.println("Disabled attribute: " + button.getAttribute("disabled"));</code></pre>

<p>When the testing requirement is specifically to verify whether a form control is enabled, <strong>isEnabled()</strong> is generally the clearer semantic check.</p>

<hr>

<h2>37. Disabled Attribute Example</h2>

<pre><code>&lt;button id="submitBtn" disabled&gt;Submit&lt;/button&gt;</code></pre>

<pre><code>WebElement button = driver.findElement(By.id("submitBtn"));

String disabled = button.getAttribute("disabled");

boolean enabled = button.isEnabled();

System.out.println("disabled attribute: " + disabled);

System.out.println("enabled: " + enabled);</code></pre>

<hr>

<h2>38. Disabled Fieldset</h2>

<p>HTML can disable groups of form controls through a disabled <strong>fieldset</strong>. In such cases, the child control may not itself contain a <strong>disabled</strong> attribute even though its effective enabled state is affected.</p>

<pre><code>&lt;fieldset disabled&gt;

    &lt;input id="username"&gt;

    &lt;button id="submit"&gt;Submit&lt;/button&gt;

&lt;/fieldset&gt;</code></pre>

<p>This is one reason a semantic enabled-state check is preferable when the test intent is to verify whether the control can be enabled for interaction.</p>

<hr>

<h2>39. readonly vs disabled</h2>

<p><strong>readonly</strong> and <strong>disabled</strong> are different concepts.</p>

<pre><code>&lt;input id="username" readonly&gt;

&lt;input id="password" disabled&gt;</code></pre>

<p>A readonly text field can remain enabled according to WebDriver while preventing the user from editing its value. A disabled control is not enabled for normal interaction.</p>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>State</th><th>Meaning</th></tr>

<tr><td>readonly</td><td>Value generally cannot be edited by the user.</td></tr>

<tr><td>disabled</td><td>Control is disabled for normal form interaction.</td></tr>

</table>

<hr>

<h2>40. aria-disabled vs disabled</h2>

<p>Modern applications may use <strong>aria-disabled="true"</strong> to communicate an application's disabled state.</p>

<pre><code>&lt;button aria-disabled="true"&gt;Submit&lt;/button&gt;</code></pre>

<p>ARIA state and native HTML disabled state are not identical. If an application uses ARIA-only state management, the automation test may need to verify the relevant application attribute or behavior separately.</p>

<hr>

<h2>41. isEnabled() Does Not Guarantee Successful Click</h2>

<p>An element can be enabled and still fail when Selenium tries to click it. For example, another element may cover it, the DOM may change, the element may become stale, or the application may have another interaction constraint.</p>

<pre><code>WebElement button = driver.findElement(By.id("submit"));

if(button.isEnabled()){

    button.click();

}</code></pre>

<p>The condition confirms enabled state, but it does not guarantee that the subsequent click will succeed in every situation.</p>

<hr>

<h2>42. isEnabled() and elementToBeClickable</h2>

<p>When the objective is specifically to click an element, Selenium's explicit wait condition <strong>elementToBeClickable()</strong> is often more suitable.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement button = wait.until(

    ExpectedConditions.elementToBeClickable(By.id("submit"))

);

button.click();</code></pre>

<p>This condition is intended for an element that is visible and enabled for clicking.</p>

<hr>

<h2>43. NoSuchElementException</h2>

<p>If the locator cannot find the element, Selenium does not reach the <strong>isEnabled()</strong> call. The <strong>findElement()</strong> operation can throw <strong>NoSuchElementException</strong>.</p>

<pre><code>WebElement button = driver.findElement(By.id("submit"));

boolean enabled = button.isEnabled();</code></pre>

<p>The element must first be successfully located before its enabled state can be checked.</p>

<hr>

<h2>44. Handling Dynamic Elements</h2>

<p>For elements that appear dynamically, explicit waits are generally preferable to immediately calling <strong>isEnabled()</strong>.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement button = wait.until(

    ExpectedConditions.elementToBeClickable(By.id("submit"))

);

button.click();</code></pre>

<hr>

<h2>45. StaleElementReferenceException</h2>

<p>Modern applications may rebuild elements through JavaScript or framework rendering. If a stored WebElement refers to an old DOM node, Selenium may throw <strong>StaleElementReferenceException</strong>.</p>

<pre><code>WebElement button = driver.findElement(By.id("submit"));

driver.navigate().refresh();

boolean enabled = button.isEnabled();</code></pre>

<p>In dynamic situations, locating the element again can be safer.</p>

<hr>

<h2>46. Safer Dynamic Check</h2>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(driver -&gt; driver.findElement(By.id("submit")).isEnabled());</code></pre>

<hr>

<h2>47. Page Object Model with isEnabled()</h2>

<p>In the Page Object Model, enabled-state checks can be encapsulated inside page classes.</p>

<pre><code>public boolean isLoginButtonEnabled(){

    return loginButton.isEnabled();

}</code></pre>

<p>The test class can then use:</p>

<pre><code>Assert.assertTrue(loginPage.isLoginButtonEnabled());</code></pre>

<hr>

<h2>48. Complete Page Object Example</h2>

<pre><code>public class LoginPage {

    private WebDriver driver;

    private By username = By.id("username");

    private By password = By.id("password");

    private By loginButton = By.id("loginBtn");

 

    public LoginPage(WebDriver driver){

        this.driver = driver;

    }

 

    public void enterUsername(String value){

        driver.findElement(username).sendKeys(value);

    }

 

    public void enterPassword(String value){

        driver.findElement(password).sendKeys(value);

    }

 

    public boolean isLoginButtonEnabled(){

        return driver.findElement(loginButton).isEnabled();

    }

 

    public void clickLogin(){

        driver.findElement(loginButton).click();

    }

}</code></pre>

<hr>

<h2>49. Test Class Example</h2>

<pre><code>LoginPage loginPage = new LoginPage(driver);

loginPage.enterUsername("admin");

loginPage.enterPassword("admin123");

Assert.assertTrue(loginPage.isLoginButtonEnabled());

loginPage.clickLogin();</code></pre>

<hr>

<h2>50. Dynamic Login Workflow</h2>

<pre><code>Open Login Page

      ↓

Enter Username

      ↓

Enter Password

      ↓

Application Validates Credentials

      ↓

Login Button Becomes Enabled

      ↓

Check isEnabled()

      ↓

Click Login Button</code></pre>

<hr>

<h2>51. Registration Form Workflow</h2>

<pre><code>Open Registration Form

      ↓

Enter Required Details

      ↓

Accept Terms

      ↓

Client-Side Validation

      ↓

Submit Button Enabled

      ↓

Verify isEnabled()

      ↓

Submit Form</code></pre>

<hr>

<h2>52. E-Commerce Checkout Workflow</h2>

<pre><code>Add Product to Cart

      ↓

Open Cart

      ↓

Enter Shipping Details

      ↓

Select Delivery Method

      ↓

Application Validates Information

      ↓

Checkout Button Enabled

      ↓

Verify isEnabled()

      ↓

Proceed to Checkout</code></pre>

<hr>

<h2>53. Checking Several Form Controls</h2>

<pre><code>WebElement name = driver.findElement(By.id("name"));

WebElement email = driver.findElement(By.id("email"));

WebElement phone = driver.findElement(By.id("phone"));

WebElement submit = driver.findElement(By.id("submit"));

 

System.out.println("Name enabled: " + name.isEnabled());

System.out.println("Email enabled: " + email.isEnabled());

System.out.println("Phone enabled: " + phone.isEnabled());

System.out.println("Submit enabled: " + submit.isEnabled());</code></pre>

<hr>

<h2>54. Conditional Interaction</h2>

<pre><code>WebElement nextButton = driver.findElement(By.id("next"));

if(nextButton.isEnabled()){

    nextButton.click();

}else{

    System.out.println("Next button is not enabled yet");

}</code></pre>

<hr>

<h2>55. Checking Element State Before sendKeys()</h2>

<pre><code>WebElement username = driver.findElement(By.id("username"));

if(username.isEnabled()){

    username.clear();

    username.sendKeys("admin");

}</code></pre>

<hr>

<h2>56. Checking Element State Before Selecting</h2>

<pre><code>WebElement country = driver.findElement(By.id("country"));

if(country.isEnabled()){

    Select select = new Select(country);

    select.selectByVisibleText("India");

}</code></pre>

<hr>

<h2>57. Using isEnabled() with Explicit Wait</h2>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));

wait.until(driver -&gt; {

    WebElement submit = driver.findElement(By.id("submit"));

    return submit.isDisplayed() &amp;&amp; submit.isEnabled();

});</code></pre>

<p>For a fully dynamic element that can be replaced in the DOM, locating it again inside the condition is safer than relying on an old WebElement reference.</p>

<hr>

<h2>58. Checking Visibility and Enabled State Together</h2>

<pre><code>WebElement button = driver.findElement(By.id("submit"));

boolean ready = button.isDisplayed() &amp;&amp; button.isEnabled();

System.out.println("Button ready: " + ready);</code></pre>

<hr>

<h2>59. Optional Elements</h2>

<p>When an element may not exist on a page, <strong>findElements()</strong> can be useful because it returns an empty list instead of throwing <strong>NoSuchElementException</strong>.</p>

<pre><code>List&lt;WebElement&gt; buttons = driver.findElements(By.id("submit"));

if(!buttons.isEmpty()){

    System.out.println("Enabled: " + buttons.get(0).isEnabled());

}else{

    System.out.println("Submit button is not present");

}</code></pre>

<hr>

<h2>60. Checking Disabled State with TestNG</h2>

<pre><code>WebElement submit = driver.findElement(By.id("submit"));

Assert.assertFalse(submit.isEnabled());</code></pre>

<hr>

<h2>61. Checking Enabled State with TestNG</h2>

<pre><code>WebElement submit = driver.findElement(By.id("submit"));

Assert.assertTrue(submit.isEnabled());</code></pre>

<hr>

<h2>62. Real-World Validation Scenario</h2>

<p>Suppose a registration form contains Name, Email, Password, Confirm Password, and Submit controls. The Submit button is disabled until all required fields are valid.</p>

<pre><code>WebElement submit = driver.findElement(By.id("submit"));

Assert.assertFalse(submit.isEnabled());

 

driver.findElement(By.id("name")).sendKeys("Amit");

driver.findElement(By.id("email")).sendKeys("[email protected]");

driver.findElement(By.id("password")).sendKeys("Password123");

driver.findElement(By.id("confirmPassword")).sendKeys("Password123");

 

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(driver -&gt; driver.findElement(By.id("submit")).isEnabled());

 

Assert.assertTrue(driver.findElement(By.id("submit")).isEnabled());</code></pre>

<hr>

<h2>63. Practical Project Example</h2>

<p>A practical Selenium project can validate an e-commerce checkout process.</p>

<ol>

<li>Open the shopping application.</li>

<li>Login with valid credentials.</li>

<li>Search for a product.</li>

<li>Add the product to the cart.</li>

<li>Open the cart.</li>

<li>Enter shipping details.</li>

<li>Check whether the Checkout button is enabled.</li>

<li>Proceed only when the button is enabled.</li>

<li>Verify the payment control state.</li>

<li>Complete the transaction workflow.</li>

</ol>

<hr>

<h2>64. Complete Practical Example</h2>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

 

WebElement checkout = wait.until(

    ExpectedConditions.visibilityOfElementLocated(By.id("checkout"))

);

 

boolean enabled = checkout.isEnabled();

 

if(enabled){

    checkout.click();

    System.out.println("Checkout button clicked successfully");

}else{

    System.out.println("Checkout button is disabled");

}</code></pre>

<hr>

<h2>65. Common Mistake: Confusing Enabled with Visible</h2>

<p>A common mistake is assuming that an enabled element must also be visible.</p>

<pre><code>WebElement element = driver.findElement(By.id("submit"));

System.out.println(element.isEnabled());

System.out.println(element.isDisplayed());</code></pre>

<p>These methods answer different questions and should be used according to the test requirement.</p>

<hr>

<h2>66. Common Mistake: Assuming Enabled Means Clickable</h2>

<p>An enabled element may still have another element covering it or may be affected by a dynamic page change. Therefore, enabled state alone should not be treated as a guarantee that a click will succeed.</p>

<hr>

<h2>67. Common Mistake: Using Thread.sleep()</h2>

<p>Avoid using fixed delays as the primary solution for waiting for an element to become enabled.</p>

<pre><code>Thread.sleep(5000);

button.click();</code></pre>

<p>Instead, use an explicit wait based on the actual application condition.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement button = wait.until(

    ExpectedConditions.elementToBeClickable(By.id("submit"))

);

button.click();</code></pre>

<hr>

<h2>68. Common Mistake: Checking Attribute Instead of State</h2>

<p>Using only <strong>getAttribute("disabled")</strong> may not express the actual test requirement. If the requirement is "verify whether the control is enabled," use <strong>isEnabled()</strong>.</p>

<hr>

<h2>69. Common Mistake: Calling isEnabled() Before Locating Element</h2>

<p><strong>isEnabled()</strong> belongs to WebElement, so Selenium must first locate the element.</p>

<pre><code>WebElement button = driver.findElement(By.id("submit"));

boolean enabled = button.isEnabled();</code></pre>

<hr>

<h2>70. Best Practices for isEnabled()</h2>

<ul>

<li>Use <strong>isEnabled()</strong> when the test requirement is about enabled or disabled state.</li>

<li>Use <strong>isDisplayed()</strong> separately when visibility matters.</li>

<li>Use <strong>isSelected()</strong> for selection state.</li>

<li>Use explicit waits for dynamically changing states.</li>

<li>Prefer stable locators such as IDs, reliable CSS selectors, or well-designed XPath expressions.</li>

<li>Use Page Object Model methods to encapsulate element-state checks.</li>

<li>Use TestNG or JUnit assertions to validate expected states.</li>

<li>Avoid unnecessary Thread.sleep() calls.</li>

<li>Remember that enabled state does not guarantee successful interaction.</li>

<li>Re-locate elements when dynamic DOM updates can make stored references stale.</li>

</ul>

<hr>

<h2>71. Advantages of isEnabled()</h2>

<ul>

<li>Simple and easy to use.</li>

<li>Returns a clear boolean result.</li>

<li>Useful for form validation testing.</li>

<li>Useful for dynamic UI testing.</li>

<li>Helps verify button states.</li>

<li>Works with Selenium WebElement objects.</li>

<li>Can be combined with explicit waits.</li>

<li>Useful in Page Object Model frameworks.</li>

<li>Useful with TestNG and JUnit assertions.</li>

<li>Supports practical end-to-end automation scenarios.</li>

</ul>

<hr>

<h2>72. Limitations and Considerations</h2>

<ul>

<li>It checks enabled state, not visibility.</li>

<li>It does not automatically wait for state changes.</li>

<li>It does not guarantee a successful click.</li>

<li>It can throw a stale element exception if the referenced DOM node is replaced.</li>

<li>ARIA-based application states may need separate validation.</li>

<li>Application-specific business rules may require additional checks.</li>

</ul>

<hr>

<h2>73. Quick Comparison of WebElement State Methods</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Method</th><th>Question Answered</th><th>Example</th></tr>

<tr><td>isDisplayed()</td><td>Is the element displayed?</td><td>Verify error message visibility</td></tr>

<tr><td>isEnabled()</td><td>Is the element enabled?</td><td>Verify Submit button state</td></tr>

<tr><td>isSelected()</td><td>Is the element selected?</td><td>Verify checkbox state</td></tr>

<tr><td>getText()</td><td>What visible text does it contain?</td><td>Read heading text</td></tr>

<tr><td>getAttribute()</td><td>What attribute/property-related value is exposed?</td><td>Read class or value</td></tr>

<tr><td>getSize()</td><td>What is the element's size?</td><td>Read width and height</td></tr>

<tr><td>getLocation()</td><td>Where is the element located?</td><td>Read x and y coordinates</td></tr>

</table>

<hr>

<h2>74. Interview Question: What is isEnabled() in Selenium?</h2>

<p><strong>Answer:</strong> isEnabled() is a Selenium WebElement method used to determine whether an element is enabled. It returns a boolean value. It is commonly used with buttons, input fields, checkboxes, radio buttons, and other form controls.</p>

<hr>

<h2>75. Interview Question: What does isEnabled() return?</h2>

<p><strong>Answer:</strong> It returns a boolean value. It returns <strong>true</strong> when the element is enabled and <strong>false</strong> when the element is disabled.</p>

<hr>

<h2>76. Interview Question: What is the difference between isEnabled() and isDisplayed()?</h2>

<p><strong>Answer:</strong> isEnabled() checks whether the element is enabled, while isDisplayed() checks whether the element is displayed according to WebDriver's displayedness semantics.</p>

<hr>

<h2>77. Interview Question: Does isEnabled() click an element?</h2>

<p><strong>Answer:</strong> No. isEnabled() only checks the enabled state. It does not perform any interaction. The <strong>click()</strong> method must be used to click an element.</p>

<hr>

<h2>78. Interview Question: Does isEnabled() wait for an element?</h2>

<p><strong>Answer:</strong> No. isEnabled() performs an immediate state check. For dynamic applications, use WebDriverWait and an appropriate expected condition or custom condition.</p>

<hr>

<h2>79. Interview Question: Can an enabled element still fail to click?</h2>

<p><strong>Answer:</strong> Yes. An enabled element may still be affected by overlays, layout changes, stale references, or other application conditions. Therefore, isEnabled() should not be treated as a complete guarantee of click success.</p>

<hr>

<h2>80. Interview Question: What is the difference between disabled and readonly?</h2>

<p><strong>Answer:</strong> A disabled control is not enabled for normal interaction, while a readonly field can generally remain enabled but does not allow the user to edit its value in the normal way.</p>

<hr>

<h2>81. Interview Question: How do you wait until an element becomes enabled?</h2>

<p><strong>Answer:</strong> For a click operation, use an explicit wait with <strong>ExpectedConditions.elementToBeClickable()</strong>. When the requirement is specifically the enabled state, a custom wait condition can check <strong>isEnabled()</strong>.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(driver -&gt; driver.findElement(By.id("submit")).isEnabled());</code></pre>

<hr>

<h2>82. Interview Question: How do you verify that a button is disabled?</h2>

<pre><code>WebElement button = driver.findElement(By.id("submit"));

Assert.assertFalse(button.isEnabled());</code></pre>

<hr>

<h2>83. Interview Question: How do you verify that a button is enabled?</h2>

<pre><code>WebElement button = driver.findElement(By.id("submit"));

Assert.assertTrue(button.isEnabled());</code></pre>

<hr>

<h2>84. Interview Question: Can isEnabled() be used with XPath?</h2>

<p><strong>Answer:</strong> Yes. Selenium first locates the element using XPath and then calls isEnabled().</p>

<pre><code>WebElement button = driver.findElement(

    By.xpath("//button[@type='submit']")

);

boolean enabled = button.isEnabled();</code></pre>

<hr>

<h2>85. Interview Question: Can isEnabled() be used with CSS Selector?</h2>

<p><strong>Answer:</strong> Yes.</p>

<pre><code>WebElement button = driver.findElement(

    By.cssSelector("button[type='submit']")

);

boolean enabled = button.isEnabled();</code></pre>

<hr>

<h2>86. Interview Question: Which Selenium interface provides isEnabled()?</h2>

<p><strong>Answer:</strong> The <strong>WebElement</strong> interface provides the <strong>isEnabled()</strong> method in Selenium WebDriver.</p>

<hr>

<h2>87. Practical Checklist</h2>

<ul>

<li>Locate the element correctly.</li>

<li>Call <strong>isEnabled()</strong> on the WebElement.</li>

<li>Store the returned boolean when needed.</li>

<li>Use <strong>isDisplayed()</strong> separately for visibility.</li>

<li>Use <strong>isSelected()</strong> separately for selection.</li>

<li>Use explicit waits for dynamic enabled states.</li>

<li>Use assertions for expected states.</li>

<li>Handle dynamic DOM changes carefully.</li>

<li>Do not assume enabled means click will always succeed.</li>

<li>Use Page Object Model for maintainable automation frameworks.</li>

</ul>

<hr>

<h2>88. Complete Selenium Example</h2>

<pre><code>import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

 

public class IsEnabledExample {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

 

        driver.get("https://example.com");

 

        WebDriverWait wait = new WebDriverWait(

            driver,

            Duration.ofSeconds(10)

        );

 

        WebElement submit = wait.until(

            ExpectedConditions.visibilityOfElementLocated(

                By.id("submit")

            )

        );

 

        if(submit.isEnabled()){

            System.out.println("Submit button is enabled");

        }else{

            System.out.println("Submit button is disabled");

        }

 

        driver.quit();

    }

}</code></pre>

<hr>

<h2>89. Real-World Automation Flow</h2>

<pre><code>Start Test

    ↓

Open Web Application

    ↓

Locate Web Element

    ↓

Check isDisplayed()

    ↓

Check isEnabled()

    ↓

If Enabled

    ↓

Perform Interaction

    ↓

Verify Result

    ↓

Continue Test

    ↓

End Test</code></pre>

<hr>

<h2>90. Summary</h2>

<p><strong>isEnabled()</strong> is an important Selenium WebElement method for validating whether an element is enabled for interaction. It returns a boolean value and is especially useful for testing dynamic buttons, input fields, checkboxes, radio buttons, select controls, and form submission workflows.</p>

<p>For reliable automation, remember that <strong>isEnabled()</strong> checks enabled state only. It does not replace <strong>isDisplayed()</strong>, <strong>isSelected()</strong>, explicit waits, or application-specific validation. When a control changes state dynamically, use an appropriate explicit wait and re-locate elements when necessary to handle DOM updates safely.</p>

<p>For structured Selenium learning covering WebDriver, WebElements, locators, TestNG, automation frameworks, Page Object Model, data-driven testing, and real-time projects, visit <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">JustAcademy Selenium Training</a>.</p>

<p>For a practical introduction or course demonstration, visit <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Selenium Course Demo</a>.</p>

</div>

Previous Next
whatsapp