Popular Searches
Popular Course Categories
Popular Courses

Is Selected

Working with Web Elements

<div class="selenium-notes">

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

<p><strong>isSelected()</strong> is a Selenium WebDriver method used to check whether a web element is currently selected. It returns a boolean value: <strong>true</strong> when the element is selected and <strong>false</strong> when it is not selected.</p>

<p>The method is mainly useful for elements that have a selection state, such as <strong>checkboxes</strong>, <strong>radio buttons</strong>, and <strong>&lt;option&gt;</strong> elements inside HTML select controls. It allows automation testers to verify the current state of these controls before or after performing an interaction.</p>

<p>JustAcademy's Selenium Automation Testing Course with Java covers Selenium WebDriver, locators and WebElements, TestNG, data-driven testing, Page Object Model, cross-browser testing, automation frameworks, test execution, reporting, and real-time automation projects. <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training Course</a></p>

<p>For a practical course demonstration, 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 isSelected()?</h2>

<p><strong>isSelected()</strong> is a method of Selenium's <strong>WebElement</strong> interface. It checks whether the current element is selected.</p>

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

<ul>

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

<li><strong>false</strong> → The element is not selected.</li>

</ul>

<p>It is commonly used with checkboxes, radio buttons, and option elements.</p>

<hr>

<h2>2. Basic Syntax</h2>

<pre><code>boolean selected = element.isSelected();</code></pre>

<p>Here, <strong>element</strong> is the Selenium WebElement whose selection state needs to be checked.</p>

<hr>

<h2>3. Simple Example</h2>

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

boolean selected = checkbox.isSelected();

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

<p>If the checkbox is selected, the output will be:</p>

<pre><code>Checkbox selected: true</code></pre>

<p>If it is not selected, the output will be:</p>

<pre><code>Checkbox selected: false</code></pre>

<hr>

<h2>4. Return Type of isSelected()</h2>

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

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

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

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

<tr><td>false</td><td>The element is not selected.</td></tr>

</table>

<hr>

<h2>5. Where is isSelected() Commonly Used?</h2>

<p><strong>isSelected()</strong> is mainly used with elements that have a selection state.</p>

<ul>

<li>Checkboxes</li>

<li>Radio buttons</li>

<li>HTML select options</li>

<li>Filter selections</li>

<li>Notification preferences</li>

<li>Permission controls</li>

<li>Terms and conditions checkboxes</li>

<li>Product attribute selections</li>

<li>Gender selection</li>

<li>Shipping or delivery options</li>

</ul>

<hr>

<h2>6. isSelected() with Checkbox</h2>

<p>A checkbox is one of the most common elements for using <strong>isSelected()</strong>.</p>

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

System.out.println(checkbox.isSelected());</code></pre>

<p>If the checkbox is checked, Selenium returns <strong>true</strong>. Otherwise, it returns <strong>false</strong>.</p>

<hr>

<h2>7. HTML Checkbox Example</h2>

<pre><code>&lt;input type="checkbox" id="subscribe"&gt;

&lt;label for="subscribe"&gt;Subscribe to newsletter&lt;/label&gt;</code></pre>

<p>Selenium code:</p>

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

boolean selected = subscribe.isSelected();

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

<hr>

<h2>8. Selecting a Checkbox Using isSelected()</h2>

<p>A common automation pattern is to check whether a checkbox is already selected before clicking it.</p>

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

if(!checkbox.isSelected()){

    checkbox.click();

}</code></pre>

<p>This prevents unnecessary clicking when the checkbox is already selected.</p>

<hr>

<h2>9. Deselecting a Checkbox Using isSelected()</h2>

<p>The same technique can be used when the test requires the checkbox to be unselected.</p>

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

if(checkbox.isSelected()){

    checkbox.click();

}</code></pre>

<hr>

<h2>10. Verify Checkbox Is Selected</h2>

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

Assert.assertTrue(checkbox.isSelected());</code></pre>

<p>This assertion passes when the checkbox is selected.</p>

<hr>

<h2>11. Verify Checkbox Is Not Selected</h2>

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

Assert.assertFalse(checkbox.isSelected());</code></pre>

<hr>

<h2>12. isSelected() with Radio Button</h2>

<p>Radio buttons are another common use case.</p>

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

System.out.println("Male selected: " + male.isSelected());</code></pre>

<hr>

<h2>13. Radio Button HTML Example</h2>

<pre><code>&lt;input type="radio" id="male" name="gender" value="male"&gt;

&lt;label for="male"&gt;Male&lt;/label&gt;

&lt;input type="radio" id="female" name="gender" value="female"&gt;

&lt;label for="female"&gt;Female&lt;/label&gt;</code></pre>

<hr>

<h2>14. Verify Selected Radio Button</h2>

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

male.click();

Assert.assertTrue(male.isSelected());</code></pre>

<hr>

<h2>15. Verify Radio Button Is Not Selected</h2>

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

Assert.assertFalse(female.isSelected());</code></pre>

<hr>

<h2>16. Checking Multiple Radio Buttons</h2>

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

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

if(male.isSelected()){

    System.out.println("Male is selected");

}

if(female.isSelected()){

    System.out.println("Female is selected");

}</code></pre>

<hr>

<h2>17. isSelected() with HTML Option</h2>

<p><strong>isSelected()</strong> can also be used with an <strong>&lt;option&gt;</strong> element inside an HTML select control.</p>

<pre><code>&lt;select id="country"&gt;

    &lt;option value="india"&gt;India&lt;/option&gt;

    &lt;option value="usa"&gt;USA&lt;/option&gt;

    &lt;option value="uk"&gt;United Kingdom&lt;/option&gt;

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

<hr>

<h2>18. Checking Selected Option</h2>

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

    By.xpath("//select[@id='country']/option[@value='india']")

);

System.out.println(india.isSelected());</code></pre>

<hr>

<h2>19. Select Class with isSelected()</h2>

<p>Selenium's <strong>Select</strong> class provides convenient methods for working with HTML select elements.</p>

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

Select select = new Select(country);

select.selectByVisibleText("India");

 

WebElement selectedOption = select.getFirstSelectedOption();

System.out.println(selectedOption.isSelected());</code></pre>

<hr>

<h2>20. getFirstSelectedOption()</h2>

<p><strong>getFirstSelectedOption()</strong> returns the currently selected option from a select element.</p>

<pre><code>Select select = new Select(driver.findElement(By.id("country")));

WebElement selectedOption = select.getFirstSelectedOption();

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

<hr>

<h2>21. Select by Visible Text and Verify</h2>

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

Select select = new Select(country);

select.selectByVisibleText("India");

 

WebElement selectedOption = select.getFirstSelectedOption();

Assert.assertEquals(selectedOption.getText(), "India");

Assert.assertTrue(selectedOption.isSelected());</code></pre>

<hr>

<h2>22. Select by Value and Verify</h2>

<pre><code>Select select = new Select(

    driver.findElement(By.id("country"))

);

select.selectByValue("india");

 

WebElement option = select.getFirstSelectedOption();

Assert.assertTrue(option.isSelected());</code></pre>

<hr>

<h2>23. Select by Index and Verify</h2>

<pre><code>Select select = new Select(

    driver.findElement(By.id("country"))

);

select.selectByIndex(1);

 

WebElement option = select.getFirstSelectedOption();

System.out.println(option.getText());

System.out.println(option.isSelected());</code></pre>

<hr>

<h2>24. isSelected() Does Not Select an Element</h2>

<p>A very important point is that <strong>isSelected()</strong> only checks the current state. It does not select the element.</p>

<pre><code>boolean selected = checkbox.isSelected();</code></pre>

<p>The above code only reads the state. To change the state of a checkbox or radio button, an interaction such as <strong>click()</strong> is normally required.</p>

<hr>

<h2>25. isSelected() vs click()</h2>

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

<tr><th>Method</th><th>Purpose</th></tr>

<tr><td>isSelected()</td><td>Checks whether the element is selected.</td></tr>

<tr><td>click()</td><td>Interacts with the element and may change its state.</td></tr>

</table>

<hr>

<h2>26. isSelected() vs isEnabled()</h2>

<p>These two methods check different element states.</p>

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

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

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

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

<tr><th>Method</th><th>Checks</th></tr>

<tr><td>isEnabled()</td><td>Whether the element is enabled.</td></tr>

<tr><td>isSelected()</td><td>Whether the element is selected.</td></tr>

</table>

<hr>

<h2>27. isSelected() vs isDisplayed()</h2>

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

System.out.println("Displayed: " + checkbox.isDisplayed());

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

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

<tr><th>Method</th><th>Purpose</th></tr>

<tr><td>isDisplayed()</td><td>Checks displayedness.</td></tr>

<tr><td>isSelected()</td><td>Checks selection state.</td></tr>

</table>

<hr>

<h2>28. Combining Displayed, Enabled and Selected</h2>

<p>Sometimes an automation test needs to verify several states at the same time.</p>

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

boolean displayed = checkbox.isDisplayed();

boolean enabled = checkbox.isEnabled();

boolean selected = checkbox.isSelected();

 

System.out.println("Displayed: " + displayed);

System.out.println("Enabled: " + enabled);

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

<hr>

<h2>29. Checking a Checkbox Before Clicking</h2>

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

if(!terms.isSelected()){

    terms.click();

}</code></pre>

<p>This is one of the most useful practical patterns when the expected state is selected.</p>

<hr>

<h2>30. Checking Checkbox After Clicking</h2>

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

terms.click();

Assert.assertTrue(terms.isSelected());</code></pre>

<hr>

<h2>31. Toggle Checkbox Safely</h2>

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

if(checkbox.isSelected()){

    checkbox.click();

}else{

    checkbox.click();

}

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

<p>The above example demonstrates that clicking a checkbox can toggle its state. In real automation, the desired final state should be explicitly defined before deciding whether to click.</p>

<hr>

<h2>32. Set Checkbox to Selected State</h2>

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

if(!checkbox.isSelected()){

    checkbox.click();

}

Assert.assertTrue(checkbox.isSelected());</code></pre>

<hr>

<h2>33. Set Checkbox to Unselected State</h2>

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

if(checkbox.isSelected()){

    checkbox.click();

}

Assert.assertFalse(checkbox.isSelected());</code></pre>

<hr>

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

<p>A registration page commonly contains a terms and conditions checkbox.</p>

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

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

 

if(!terms.isSelected()){

    terms.click();

}

 

if(terms.isSelected()){

    System.out.println("Terms accepted");

}

 

if(register.isEnabled()){

    register.click();

}</code></pre>

<hr>

<h2>35. Remember Me Checkbox Example</h2>

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

if(!rememberMe.isSelected()){

    rememberMe.click();

}

Assert.assertTrue(rememberMe.isSelected());</code></pre>

<hr>

<h2>36. Notification Preference Example</h2>

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

    By.id("emailNotification")

);

if(!emailNotification.isSelected()){

    emailNotification.click();

}

System.out.println(

    "Email notification enabled: " + emailNotification.isSelected()

);</code></pre>

<hr>

<h2>37. Product Filter Example</h2>

<p>E-commerce websites often contain filter checkboxes for brand, category, size, color, or other product attributes.</p>

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

if(!brandFilter.isSelected()){

    brandFilter.click();

}

Assert.assertTrue(brandFilter.isSelected());</code></pre>

<hr>

<h2>38. Multiple Checkbox Validation</h2>

<pre><code>List&lt;WebElement&gt; checkboxes = driver.findElements(

    By.cssSelector("input[type='checkbox']")

);

 

for(WebElement checkbox : checkboxes){

    System.out.println(

        "Selected: " + checkbox.isSelected()

    );

}</code></pre>

<hr>

<h2>39. Select All Required Checkboxes</h2>

<pre><code>List&lt;WebElement&gt; checkboxes = driver.findElements(

    By.cssSelector(".required-checkbox")

);

 

for(WebElement checkbox : checkboxes){

    if(!checkbox.isSelected()){

        checkbox.click();

    }

}</code></pre>

<hr>

<h2>40. Verify All Required Checkboxes</h2>

<pre><code>List&lt;WebElement&gt; checkboxes = driver.findElements(

    By.cssSelector(".required-checkbox")

);

 

for(WebElement checkbox : checkboxes){

    Assert.assertTrue(checkbox.isSelected());

}</code></pre>

<hr>

<h2>41. Radio Button Selection Workflow</h2>

<pre><code>Locate Radio Button

      ↓

Check isSelected()

      ↓

If Not Selected

      ↓

Click Radio Button

      ↓

Check isSelected() Again

      ↓

Verify Expected State</code></pre>

<hr>

<h2>42. Checkbox Selection Workflow</h2>

<pre><code>Locate Checkbox

      ↓

Check Current State

      ↓

isSelected()

      ↓

Already Selected?

   ↙           ↘

 Yes            No

 ↓              ↓

Continue       Click

                ↓

          Verify Selected</code></pre>

<hr>

<h2>43. isSelected() with XPath</h2>

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

    By.xpath("//input[@type='checkbox' and @id='terms']")

);

System.out.println(checkbox.isSelected());</code></pre>

<hr>

<h2>44. isSelected() with CSS Selector</h2>

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

    By.cssSelector("input#terms")

);

System.out.println(checkbox.isSelected());</code></pre>

<hr>

<h2>45. XPath for Selected Radio Button</h2>

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

    By.xpath("//input[@type='radio' and @value='male']")

);

Assert.assertTrue(radio.isSelected());</code></pre>

<hr>

<h2>46. isSelected() and checked Attribute</h2>

<p>Checkboxes and radio buttons can have HTML markup involving the <strong>checked</strong> attribute.</p>

<pre><code>&lt;input type="checkbox" id="subscribe" checked&gt;</code></pre>

<p>Although <strong>getAttribute("checked")</strong> can be used to inspect attribute/property-related information, <strong>isSelected()</strong> is the Selenium method intended to check the element's selected state.</p>

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

System.out.println("Selected: " + checkbox.isSelected());

System.out.println("Checked value: " + checkbox.getAttribute("checked"));</code></pre>

<hr>

<h2>47. Why Prefer isSelected() for Selection Validation?</h2>

<p>When the test requirement is to verify whether a checkbox, radio button, or option is selected, <strong>isSelected()</strong> directly expresses that testing intent.</p>

<p>For example:</p>

<pre><code>Assert.assertTrue(checkbox.isSelected());</code></pre>

<p>This is clearer than trying to infer selection only from an HTML attribute.</p>

<hr>

<h2>48. isSelected() Is an Immediate State Check</h2>

<p><strong>isSelected()</strong> does not automatically wait for an element to become selected.</p>

<pre><code>boolean selected = checkbox.isSelected();</code></pre>

<p>If the application changes the selection state asynchronously, the test may need an explicit wait.</p>

<hr>

<h2>49. Waiting Until an Element Is Selected</h2>

<p>Selenium Java provides expected conditions for selection state.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(

    driver,

    Duration.ofSeconds(10)

);

 

wait.until(

    ExpectedConditions.elementToBeSelected(By.id("terms"))

);</code></pre>

<hr>

<h2>50. Waiting for Selected State with WebElement</h2>

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

 

WebDriverWait wait = new WebDriverWait(

    driver,

    Duration.ofSeconds(10)

);

 

wait.until(

    ExpectedConditions.elementToBeSelected(checkbox)

);</code></pre>

<hr>

<h2>51. Waiting for a Specific Selection State</h2>

<p>Selenium also provides selection-state conditions that can wait for an element to be either selected or unselected.</p>

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

 

WebDriverWait wait = new WebDriverWait(

    driver,

    Duration.ofSeconds(10)

);

 

wait.until(

    ExpectedConditions.elementSelectionStateToBe(

        checkbox,

        true

    )

);</code></pre>

<hr>

<h2>52. Custom Wait with isSelected()</h2>

<p>A custom lambda can also be used when the test needs a direct <strong>isSelected()</strong> check.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(

    driver,

    Duration.ofSeconds(10)

);

 

wait.until(

    driver -&gt; driver.findElement(By.id("terms")).isSelected()

);</code></pre>

<hr>

<h2>53. Dynamic AJAX Selection</h2>

<p>Modern applications may update checkbox or radio button state through JavaScript or AJAX. In such cases, the test should wait for the expected state instead of assuming that the state changes immediately.</p>

<pre><code>WebDriverWait wait = new WebDriverWait(

    driver,

    Duration.ofSeconds(10)

);

 

wait.until(

    driver -&gt; driver.findElement(By.id("option1")).isSelected()

);</code></pre>

<hr>

<h2>54. StaleElementReferenceException</h2>

<p>Dynamic web applications can replace elements in the DOM. If Selenium holds a reference to an old element, calling <strong>isSelected()</strong> can result in <strong>StaleElementReferenceException</strong>.</p>

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

driver.navigate().refresh();

boolean selected = checkbox.isSelected();</code></pre>

<p>In dynamic scenarios, re-locating the element can be safer.</p>

<hr>

<h2>55. Safer Dynamic Selection Check</h2>

<pre><code>WebDriverWait wait = new WebDriverWait(

    driver,

    Duration.ofSeconds(10)

);

 

wait.until(

    driver -&gt; driver.findElement(By.id("terms")).isSelected()

);</code></pre>

<hr>

<h2>56. isSelected() and isEnabled() Together</h2>

<p>A checkbox can be enabled but not selected, selected but disabled, or both enabled and selected depending on the application state.</p>

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

 

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

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

<hr>

<h2>57. isSelected() and isDisplayed() Together</h2>

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

 

System.out.println("Displayed: " + checkbox.isDisplayed());

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

<hr>

<h2>58. Verify a Checkbox Is Visible, Enabled and Selected</h2>

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

 

Assert.assertTrue(checkbox.isDisplayed());

Assert.assertTrue(checkbox.isEnabled());

Assert.assertTrue(checkbox.isSelected());</code></pre>

<hr>

<h2>59. Page Object Model with isSelected()</h2>

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

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

    return termsCheckbox.isSelected();

}</code></pre>

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

<pre><code>Assert.assertTrue(registerPage.isTermsSelected());</code></pre>

<hr>

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

<pre><code>public class RegistrationPage {

    private WebDriver driver;

 

    private By termsCheckbox = By.id("terms");

    private By maleRadio = By.id("male");

 

    public RegistrationPage(WebDriver driver){

        this.driver = driver;

    }

 

    public void selectTerms(){

        WebElement terms = driver.findElement(termsCheckbox);

        if(!terms.isSelected()){

            terms.click();

        }

    }

 

    public boolean isTermsSelected(){

        return driver.findElement(termsCheckbox).isSelected();

    }

 

    public void selectMale(){

        WebElement male = driver.findElement(maleRadio);

        if(!male.isSelected()){

            male.click();

        }

    }

 

    public boolean isMaleSelected(){

        return driver.findElement(maleRadio).isSelected();

    }

}</code></pre>

<hr>

<h2>61. TestNG Test Using Page Object</h2>

<pre><code>RegistrationPage page = new RegistrationPage(driver);

 

page.selectTerms();

Assert.assertTrue(page.isTermsSelected());

 

page.selectMale();

Assert.assertTrue(page.isMaleSelected());</code></pre>

<hr>

<h2>62. Practical Registration Form Example</h2>

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

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

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

 

if(!terms.isSelected()){

    terms.click();

}

 

if(!male.isSelected()){

    male.click();

}

 

Assert.assertTrue(terms.isSelected());

Assert.assertTrue(male.isSelected());

 

if(register.isEnabled()){

    register.click();

}</code></pre>

<hr>

<h2>63. Practical E-Commerce Filter Example</h2>

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

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

 

if(!brand.isSelected()){

    brand.click();

}

 

if(!category.isSelected()){

    category.click();

}

 

Assert.assertTrue(brand.isSelected());

Assert.assertTrue(category.isSelected());</code></pre>

<hr>

<h2>64. Practical Booking Application Example</h2>

<p>A booking application may use radio buttons for selecting a payment or travel option.</p>

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

    By.id("onlinePayment")

);

 

if(!onlinePayment.isSelected()){

    onlinePayment.click();

}

 

Assert.assertTrue(onlinePayment.isSelected());</code></pre>

<hr>

<h2>65. Practical Permission Example</h2>

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

    By.id("allowNotifications")

);

 

if(!notifications.isSelected()){

    notifications.click();

}

 

Assert.assertTrue(notifications.isSelected());</code></pre>

<hr>

<h2>66. Practical Multi-Checkbox Example</h2>

<pre><code>List&lt;WebElement&gt; preferences = driver.findElements(

    By.cssSelector("input.preference")

);

 

for(WebElement preference : preferences){

    if(!preference.isSelected()){

        preference.click();

    }

}

 

for(WebElement preference : preferences){

    Assert.assertTrue(preference.isSelected());

}</code></pre>

<hr>

<h2>67. Checking Selected Option Text</h2>

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

Select select = new Select(country);

 

select.selectByVisibleText("India");

 

WebElement selectedOption = select.getFirstSelectedOption();

String text = selectedOption.getText();

 

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

<hr>

<h2>68. Checking Selected Option with isSelected()</h2>

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

Select select = new Select(country);

 

select.selectByVisibleText("India");

 

WebElement selectedOption = select.getFirstSelectedOption();

 

Assert.assertTrue(selectedOption.isSelected());

Assert.assertEquals(selectedOption.getText(), "India");</code></pre>

<hr>

<h2>69. Multiple Select Dropdown</h2>

<p>For a multi-select HTML dropdown, more than one option may be selected.</p>

<pre><code>&lt;select id="skills" multiple&gt;

    &lt;option value="java"&gt;Java&lt;/option&gt;

    &lt;option value="selenium"&gt;Selenium&lt;/option&gt;

    &lt;option value="sql"&gt;SQL&lt;/option&gt;

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

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

Select select = new Select(skills);

 

select.selectByVisibleText("Java");

select.selectByVisibleText("Selenium");

 

List&lt;WebElement&gt; selected = select.getAllSelectedOptions();

 

for(WebElement option : selected){

    System.out.println(

        option.getText() + " : " + option.isSelected()

    );

}</code></pre>

<hr>

<h2>70. Deselect All Options</h2>

<p>For a multi-select dropdown, Selenium's Select class provides <strong>deselectAll()</strong>.</p>

<pre><code>Select select = new Select(

    driver.findElement(By.id("skills"))

);

 

select.deselectAll();</code></pre>

<hr>

<h2>71. Verify Option Is Not Selected</h2>

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

    By.xpath("//select[@id='skills']/option[@value='java']")

);

 

Assert.assertFalse(javaOption.isSelected());</code></pre>

<hr>

<h2>72. Common Mistake: Calling isSelected() on Every Element</h2>

<p><strong>isSelected()</strong> is meaningful primarily for elements that have a selection state. Calling it on an ordinary heading, paragraph, div, or unrelated element does not provide a meaningful selection check.</p>

<pre><code>WebElement heading = driver.findElement(By.tagName("h1"));

System.out.println(heading.isSelected());</code></pre>

<p>For normal non-selectable elements, the result is generally <strong>false</strong>.</p>

<hr>

<h2>73. Common Mistake: Confusing Selected and Enabled</h2>

<p>A checkbox can be enabled without being 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>Do not use <strong>isEnabled()</strong> when the actual requirement is to verify selection.</p>

<hr>

<h2>74. Common Mistake: Clicking Without Checking Current State</h2>

<p>Blindly clicking a checkbox can unintentionally toggle it from selected to unselected.</p>

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

<p>A safer approach when a specific final state is required is:</p>

<pre><code>if(!checkbox.isSelected()){

    checkbox.click();

}</code></pre>

<hr>

<h2>75. Common Mistake: Using getAttribute("checked") as the Only Check</h2>

<p>For selection-state validation, use <strong>isSelected()</strong> rather than relying only on the presence or value of the HTML <strong>checked</strong> attribute.</p>

<pre><code>Assert.assertTrue(checkbox.isSelected());</code></pre>

<hr>

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

<p>A fixed delay should not be the primary mechanism for waiting for a checkbox or radio button to become selected.</p>

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

Assert.assertTrue(checkbox.isSelected());</code></pre>

<p>Prefer an explicit wait based on the actual state:</p>

<pre><code>WebDriverWait wait = new WebDriverWait(

    driver,

    Duration.ofSeconds(10)

);

 

wait.until(

    ExpectedConditions.elementToBeSelected(By.id("terms"))

);</code></pre>

<hr>

<h2>77. Best Practices for isSelected()</h2>

<ul>

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

<li>Use it primarily with checkboxes, radio buttons, and select options.</li>

<li>Check the current state before clicking a checkbox when a specific final state is required.</li>

<li>Use explicit waits for dynamic selection changes.</li>

<li>Use TestNG or JUnit assertions for expected selection states.</li>

<li>Use Page Object Model methods to encapsulate selection logic.</li>

<li>Use <strong>Select</strong> for HTML select elements.</li>

<li>Use <strong>isEnabled()</strong> separately when enabled state matters.</li>

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

<li>Be careful with stale elements in dynamically rendered applications.</li>

</ul>

<hr>

<h2>78. Advantages of isSelected()</h2>

<ul>

<li>Simple boolean-based state check.</li>

<li>Easy to use with Selenium WebElement.</li>

<li>Useful for checkbox validation.</li>

<li>Useful for radio button validation.</li>

<li>Useful for HTML select options.</li>

<li>Works well with TestNG and JUnit assertions.</li>

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

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

<li>Helps validate user selection workflows.</li>

<li>Useful in real-world form and e-commerce automation.</li>

</ul>

<hr>

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

<ul>

<li>It does not select or deselect an element.</li>

<li>It does not check whether an element is enabled.</li>

<li>It does not check whether an element is displayed.</li>

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

<li>It is primarily meaningful for selectable controls.</li>

<li>A dynamic DOM can make a stored WebElement stale.</li>

<li>Application-specific visual states may require additional checks.</li>

</ul>

<hr>

<h2>80. Quick Comparison of Selenium WebElement State Methods</h2>

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

<tr><th>Method</th><th>Purpose</th><th>Typical Usage</th></tr>

<tr><td>isDisplayed()</td><td>Checks displayedness</td><td>Verify visibility of elements</td></tr>

<tr><td>isEnabled()</td><td>Checks enabled state</td><td>Verify whether controls are enabled</td></tr>

<tr><td>isSelected()</td><td>Checks selected state</td><td>Checkboxes, radio buttons, options</td></tr>

<tr><td>click()</td><td>Interacts with an element</td><td>Select checkbox/radio or activate controls</td></tr>

<tr><td>getAttribute()</td><td>Reads attribute/property-related value</td><td>Inspect HTML state information</td></tr>

</table>

<hr>

<h2>81. Interview Question: What is isSelected() in Selenium?</h2>

<p><strong>Answer:</strong> isSelected() is a Selenium WebElement method used to determine whether an element is currently selected. It returns a boolean value and is commonly used with checkboxes, radio buttons, and HTML select options.</p>

<hr>

<h2>82. Interview Question: What does isSelected() return?</h2>

<p><strong>Answer:</strong> It returns <strong>true</strong> when the element is selected and <strong>false</strong> when it is not selected.</p>

<hr>

<h2>83. Interview Question: Does isSelected() select an element?</h2>

<p><strong>Answer:</strong> No. It only checks the current selection state. It does not perform the selection. For checkboxes and radio buttons, <strong>click()</strong> is normally used to change the state.</p>

<hr>

<h2>84. Interview Question: Which elements are commonly used with isSelected()?</h2>

<p><strong>Answer:</strong> The most common elements are checkboxes, radio buttons, and option elements inside HTML select controls.</p>

<hr>

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

<p><strong>Answer:</strong> isSelected() checks whether an element is selected, while isEnabled() checks whether an element is enabled for interaction.</p>

<hr>

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

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

<hr>

<h2>87. Interview Question: How do you select a checkbox only if it is not already selected?</h2>

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

if(!checkbox.isSelected()){

    checkbox.click();

}</code></pre>

<hr>

<h2>88. Interview Question: How do you verify that a checkbox is selected?</h2>

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

Assert.assertTrue(checkbox.isSelected());</code></pre>

<hr>

<h2>89. Interview Question: How do you verify that a checkbox is not selected?</h2>

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

Assert.assertFalse(checkbox.isSelected());</code></pre>

<hr>

<h2>90. Interview Question: How do you wait until a checkbox is selected?</h2>

<pre><code>WebDriverWait wait = new WebDriverWait(

    driver,

    Duration.ofSeconds(10)

);

 

wait.until(

    ExpectedConditions.elementToBeSelected(By.id("terms"))

);</code></pre>

<hr>

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

<p><strong>Answer:</strong> Yes. Selenium can locate the element using XPath and then call <strong>isSelected()</strong>.</p>

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

    By.xpath("//input[@id='terms']")

);

boolean selected = checkbox.isSelected();</code></pre>

<hr>

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

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

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

    By.cssSelector("#terms")

);

boolean selected = checkbox.isSelected();</code></pre>

<hr>

<h2>93. Interview Question: Can isSelected() be used with dropdown options?</h2>

<p><strong>Answer:</strong> Yes. It can be called on an individual HTML <strong>&lt;option&gt;</strong> element to check whether that option is selected.</p>

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

    By.xpath("//select[@id='country']/option[@value='india']")

);

Assert.assertTrue(option.isSelected());</code></pre>

<hr>

<h2>94. Interview Question: How can you verify a selected dropdown option?</h2>

<pre><code>Select select = new Select(

    driver.findElement(By.id("country"))

);

 

WebElement option = select.getFirstSelectedOption();

 

Assert.assertTrue(option.isSelected());

Assert.assertEquals(option.getText(), "India");</code></pre>

<hr>

<h2>95. Interview Question: Can an element be enabled but not selected?</h2>

<p><strong>Answer:</strong> Yes. For example, a checkbox can be enabled and available for interaction while currently remaining unchecked.</p>

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

System.out.println(checkbox.isSelected());</code></pre>

<hr>

<h2>96. Interview Question: Can a selected element be disabled?</h2>

<p><strong>Answer:</strong> Yes. Selection state and enabled state are separate concepts. A control can retain a selected state while being disabled by the application.</p>

<hr>

<h2>97. Practical Project Workflow</h2>

<p>A practical Selenium form automation project can use <strong>isSelected()</strong> to validate user preferences.</p>

<ol>

<li>Open the registration page.</li>

<li>Enter the user's information.</li>

<li>Locate the terms and conditions checkbox.</li>

<li>Check whether the checkbox is already selected.</li>

<li>Select it if necessary.</li>

<li>Verify that it is selected.</li>

<li>Select the required radio button.</li>

<li>Verify the radio button state.</li>

<li>Select the required dropdown option.</li>

<li>Verify the selected option.</li>

<li>Submit the form.</li>

<li>Verify the resulting page.</li>

</ol>

<hr>

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

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

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

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

 

if(!terms.isSelected()){

    terms.click();

}

 

if(!male.isSelected()){

    male.click();

}

 

Select select = new Select(country);

select.selectByVisibleText("India");

 

WebElement selectedCountry = select.getFirstSelectedOption();

 

Assert.assertTrue(terms.isSelected());

Assert.assertTrue(male.isSelected());

Assert.assertTrue(selectedCountry.isSelected());

Assert.assertEquals(selectedCountry.getText(), "India");</code></pre>

<hr>

<h2>99. isSelected() Automation Flow</h2>

<pre><code>Start Test

    ↓

Open Web Application

    ↓

Locate Checkbox / Radio / Option

    ↓

Call isSelected()

    ↓

Check Current State

    ↓

Required State?

   ↙          ↘

 Yes           No

 ↓             ↓

Continue      Perform Action

                ↓

          Check isSelected()

                ↓

              Verify

                ↓

           Continue Test</code></pre>

<hr>

<h2>100. Final Summary</h2>

<p><strong>isSelected()</strong> is an important Selenium WebElement method used to verify whether a selectable element is currently selected. It returns a boolean value and is primarily useful with checkboxes, radio buttons, and HTML select options.</p>

<p>The method only checks the current state; it does not select or deselect the element. When a specific final state is required, Selenium automation can first use <strong>isSelected()</strong> and then perform <strong>click()</strong> only when necessary.</p>

<p>For dynamic applications, remember that <strong>isSelected()</strong> is an immediate state check. Use <strong>WebDriverWait</strong> and appropriate expected conditions when the selection state changes asynchronously. Also keep selection, visibility, and enabled state separate by using <strong>isSelected()</strong>, <strong>isDisplayed()</strong>, and <strong>isEnabled()</strong> according to the testing requirement.</p>

<p>For structured Selenium learning covering Selenium WebDriver, WebElements, locators, TestNG, Page Object Model, data-driven testing, cross-browser testing, automation frameworks, 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 and 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