isSelected() in Selenium
isSelected() is a Selenium WebDriver method used to check whether a web element is currently selected. It returns a boolean value: true when the element is selected and false when it is not selected.
The method is mainly useful for elements that have a selection state, such as checkboxes, radio buttons, and <option> elements inside HTML select controls. It allows automation testers to verify the current state of these controls before or after performing an interaction.
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. Selenium Training Course
For a practical course demonstration, visit Selenium Course Demo.
1. What is isSelected()?
isSelected() is a method of Selenium's WebElement interface. It checks whether the current element is selected.
The method returns a boolean result:
- true → The element is selected.
- false → The element is not selected.
It is commonly used with checkboxes, radio buttons, and option elements.
2. Basic Syntax
boolean selected = element.isSelected();
Here, element is the Selenium WebElement whose selection state needs to be checked.
3. Simple Example
WebElement checkbox = driver.findElement(By.id("terms"));
boolean selected = checkbox.isSelected();
System.out.println("Checkbox selected: " + selected);
If the checkbox is selected, the output will be:
Checkbox selected: true
If it is not selected, the output will be:
Checkbox selected: false
4. Return Type of isSelected()
The return type of isSelected() is boolean.
| Return Value | Meaning |
| true | The element is selected. |
| false | The element is not selected. |
5. Where is isSelected() Commonly Used?
isSelected() is mainly used with elements that have a selection state.
- Checkboxes
- Radio buttons
- HTML select options
- Filter selections
- Notification preferences
- Permission controls
- Terms and conditions checkboxes
- Product attribute selections
- Gender selection
- Shipping or delivery options
6. isSelected() with Checkbox
A checkbox is one of the most common elements for using isSelected().
WebElement checkbox = driver.findElement(By.id("subscribe"));
System.out.println(checkbox.isSelected());
If the checkbox is checked, Selenium returns true. Otherwise, it returns false.
7. HTML Checkbox Example
<input type="checkbox" id="subscribe">
<label for="subscribe">Subscribe to newsletter</label>
Selenium code:
WebElement subscribe = driver.findElement(By.id("subscribe"));
boolean selected = subscribe.isSelected();
System.out.println("Selected: " + selected);
8. Selecting a Checkbox Using isSelected()
A common automation pattern is to check whether a checkbox is already selected before clicking it.
WebElement checkbox = driver.findElement(By.id("subscribe"));
if(!checkbox.isSelected()){
checkbox.click();
}
This prevents unnecessary clicking when the checkbox is already selected.
9. Deselecting a Checkbox Using isSelected()
The same technique can be used when the test requires the checkbox to be unselected.
WebElement checkbox = driver.findElement(By.id("subscribe"));
if(checkbox.isSelected()){
checkbox.click();
}
10. Verify Checkbox Is Selected
WebElement checkbox = driver.findElement(By.id("terms"));
Assert.assertTrue(checkbox.isSelected());
This assertion passes when the checkbox is selected.
11. Verify Checkbox Is Not Selected
WebElement checkbox = driver.findElement(By.id("terms"));
Assert.assertFalse(checkbox.isSelected());
12. isSelected() with Radio Button
Radio buttons are another common use case.
WebElement male = driver.findElement(By.id("male"));
System.out.println("Male selected: " + male.isSelected());
13. Radio Button HTML Example
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
14. Verify Selected Radio Button
WebElement male = driver.findElement(By.id("male"));
male.click();
Assert.assertTrue(male.isSelected());
15. Verify Radio Button Is Not Selected
WebElement female = driver.findElement(By.id("female"));
Assert.assertFalse(female.isSelected());
16. Checking Multiple Radio Buttons
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");
}
17. isSelected() with HTML Option
isSelected() can also be used with an <option> element inside an HTML select control.
<select id="country">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="uk">United Kingdom</option>
</select>
18. Checking Selected Option
WebElement india = driver.findElement(
By.xpath("//select[@id='country']/option[@value='india']")
);
System.out.println(india.isSelected());
19. Select Class with isSelected()
Selenium's Select class provides convenient methods for working with HTML select elements.
WebElement country = driver.findElement(By.id("country"));
Select select = new Select(country);
select.selectByVisibleText("India");
WebElement selectedOption = select.getFirstSelectedOption();
System.out.println(selectedOption.isSelected());
20. getFirstSelectedOption()
getFirstSelectedOption() returns the currently selected option from a select element.
Select select = new Select(driver.findElement(By.id("country")));
WebElement selectedOption = select.getFirstSelectedOption();
System.out.println("Selected: " + selectedOption.getText());
21. Select by Visible Text and Verify
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());
22. Select by Value and Verify
Select select = new Select(
driver.findElement(By.id("country"))
);
select.selectByValue("india");
WebElement option = select.getFirstSelectedOption();
Assert.assertTrue(option.isSelected());
23. Select by Index and Verify
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());
24. isSelected() Does Not Select an Element
A very important point is that isSelected() only checks the current state. It does not select the element.
boolean selected = checkbox.isSelected();
The above code only reads the state. To change the state of a checkbox or radio button, an interaction such as click() is normally required.
25. isSelected() vs click()
| Method | Purpose |
| isSelected() | Checks whether the element is selected. |
| click() | Interacts with the element and may change its state. |
26. isSelected() vs isEnabled()
These two methods check different element states.
WebElement checkbox = driver.findElement(By.id("subscribe"));
System.out.println("Enabled: " + checkbox.isEnabled());
System.out.println("Selected: " + checkbox.isSelected());
| Method | Checks |
| isEnabled() | Whether the element is enabled. |
| isSelected() | Whether the element is selected. |
27. isSelected() vs isDisplayed()
WebElement checkbox = driver.findElement(By.id("subscribe"));
System.out.println("Displayed: " + checkbox.isDisplayed());
System.out.println("Selected: " + checkbox.isSelected());
| Method | Purpose |
| isDisplayed() | Checks displayedness. |
| isSelected() | Checks selection state. |
28. Combining Displayed, Enabled and Selected
Sometimes an automation test needs to verify several states at the same time.
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);
29. Checking a Checkbox Before Clicking
WebElement terms = driver.findElement(By.id("terms"));
if(!terms.isSelected()){
terms.click();
}
This is one of the most useful practical patterns when the expected state is selected.
30. Checking Checkbox After Clicking
WebElement terms = driver.findElement(By.id("terms"));
terms.click();
Assert.assertTrue(terms.isSelected());
31. Toggle Checkbox Safely
WebElement checkbox = driver.findElement(By.id("notifications"));
if(checkbox.isSelected()){
checkbox.click();
}else{
checkbox.click();
}
System.out.println("Final state: " + checkbox.isSelected());
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.
32. Set Checkbox to Selected State
WebElement checkbox = driver.findElement(By.id("notifications"));
if(!checkbox.isSelected()){
checkbox.click();
}
Assert.assertTrue(checkbox.isSelected());
33. Set Checkbox to Unselected State
WebElement checkbox = driver.findElement(By.id("notifications"));
if(checkbox.isSelected()){
checkbox.click();
}
Assert.assertFalse(checkbox.isSelected());
34. Terms and Conditions Example
A registration page commonly contains a terms and conditions checkbox.
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();
}
35. Remember Me Checkbox Example
WebElement rememberMe = driver.findElement(By.id("rememberMe"));
if(!rememberMe.isSelected()){
rememberMe.click();
}
Assert.assertTrue(rememberMe.isSelected());
36. Notification Preference Example
WebElement emailNotification = driver.findElement(
By.id("emailNotification")
);
if(!emailNotification.isSelected()){
emailNotification.click();
}
System.out.println(
"Email notification enabled: " + emailNotification.isSelected()
);
37. Product Filter Example
E-commerce websites often contain filter checkboxes for brand, category, size, color, or other product attributes.
WebElement brandFilter = driver.findElement(By.id("brandSamsung"));
if(!brandFilter.isSelected()){
brandFilter.click();
}
Assert.assertTrue(brandFilter.isSelected());
38. Multiple Checkbox Validation
List<WebElement> checkboxes = driver.findElements(
By.cssSelector("input[type='checkbox']")
);
for(WebElement checkbox : checkboxes){
System.out.println(
"Selected: " + checkbox.isSelected()
);
}
39. Select All Required Checkboxes
List<WebElement> checkboxes = driver.findElements(
By.cssSelector(".required-checkbox")
);
for(WebElement checkbox : checkboxes){
if(!checkbox.isSelected()){
checkbox.click();
}
}
40. Verify All Required Checkboxes
List<WebElement> checkboxes = driver.findElements(
By.cssSelector(".required-checkbox")
);
for(WebElement checkbox : checkboxes){
Assert.assertTrue(checkbox.isSelected());
}
41. Radio Button Selection Workflow
Locate Radio Button
↓
Check isSelected()
↓
If Not Selected
↓
Click Radio Button
↓
Check isSelected() Again
↓
Verify Expected State
42. Checkbox Selection Workflow
Locate Checkbox
↓
Check Current State
↓
isSelected()
↓
Already Selected?
↙ ↘
Yes No
↓ ↓
Continue Click
↓
Verify Selected
43. isSelected() with XPath
WebElement checkbox = driver.findElement(
By.xpath("//input[@type='checkbox' and @id='terms']")
);
System.out.println(checkbox.isSelected());
44. isSelected() with CSS Selector
WebElement checkbox = driver.findElement(
By.cssSelector("input#terms")
);
System.out.println(checkbox.isSelected());
45. XPath for Selected Radio Button
WebElement radio = driver.findElement(
By.xpath("//input[@type='radio' and @value='male']")
);
Assert.assertTrue(radio.isSelected());
46. isSelected() and checked Attribute
Checkboxes and radio buttons can have HTML markup involving the checked attribute.
<input type="checkbox" id="subscribe" checked>
Although getAttribute("checked") can be used to inspect attribute/property-related information, isSelected() is the Selenium method intended to check the element's selected state.
WebElement checkbox = driver.findElement(By.id("subscribe"));
System.out.println("Selected: " + checkbox.isSelected());
System.out.println("Checked value: " + checkbox.getAttribute("checked"));
47. Why Prefer isSelected() for Selection Validation?
When the test requirement is to verify whether a checkbox, radio button, or option is selected, isSelected() directly expresses that testing intent.
For example:
Assert.assertTrue(checkbox.isSelected());
This is clearer than trying to infer selection only from an HTML attribute.
48. isSelected() Is an Immediate State Check
isSelected() does not automatically wait for an element to become selected.
boolean selected = checkbox.isSelected();
If the application changes the selection state asynchronously, the test may need an explicit wait.
49. Waiting Until an Element Is Selected
Selenium Java provides expected conditions for selection state.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementToBeSelected(By.id("terms"))
);
50. Waiting for Selected State with WebElement
WebElement checkbox = driver.findElement(By.id("terms"));
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementToBeSelected(checkbox)
);
51. Waiting for a Specific Selection State
Selenium also provides selection-state conditions that can wait for an element to be either selected or unselected.
WebElement checkbox = driver.findElement(By.id("terms"));
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementSelectionStateToBe(
checkbox,
true
)
);
52. Custom Wait with isSelected()
A custom lambda can also be used when the test needs a direct isSelected() check.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
driver -> driver.findElement(By.id("terms")).isSelected()
);
53. Dynamic AJAX Selection
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.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
driver -> driver.findElement(By.id("option1")).isSelected()
);
54. StaleElementReferenceException
Dynamic web applications can replace elements in the DOM. If Selenium holds a reference to an old element, calling isSelected() can result in StaleElementReferenceException.
WebElement checkbox = driver.findElement(By.id("terms"));
driver.navigate().refresh();
boolean selected = checkbox.isSelected();
In dynamic scenarios, re-locating the element can be safer.
55. Safer Dynamic Selection Check
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
driver -> driver.findElement(By.id("terms")).isSelected()
);
56. isSelected() and isEnabled() Together
A checkbox can be enabled but not selected, selected but disabled, or both enabled and selected depending on the application state.
WebElement checkbox = driver.findElement(By.id("terms"));
System.out.println("Enabled: " + checkbox.isEnabled());
System.out.println("Selected: " + checkbox.isSelected());
57. isSelected() and isDisplayed() Together
WebElement checkbox = driver.findElement(By.id("terms"));
System.out.println("Displayed: " + checkbox.isDisplayed());
System.out.println("Selected: " + checkbox.isSelected());
58. Verify a Checkbox Is Visible, Enabled and Selected
WebElement checkbox = driver.findElement(By.id("terms"));
Assert.assertTrue(checkbox.isDisplayed());
Assert.assertTrue(checkbox.isEnabled());
Assert.assertTrue(checkbox.isSelected());
59. Page Object Model with isSelected()
In the Page Object Model, selection checks can be encapsulated inside page classes.
public boolean isTermsSelected(){
return termsCheckbox.isSelected();
}
The test class can then use:
Assert.assertTrue(registerPage.isTermsSelected());
60. Complete Page Object Example
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();
}
}
61. TestNG Test Using Page Object
RegistrationPage page = new RegistrationPage(driver);
page.selectTerms();
Assert.assertTrue(page.isTermsSelected());
page.selectMale();
Assert.assertTrue(page.isMaleSelected());
62. Practical Registration Form Example
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();
}
63. Practical E-Commerce Filter Example
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());
64. Practical Booking Application Example
A booking application may use radio buttons for selecting a payment or travel option.
WebElement onlinePayment = driver.findElement(
By.id("onlinePayment")
);
if(!onlinePayment.isSelected()){
onlinePayment.click();
}
Assert.assertTrue(onlinePayment.isSelected());
65. Practical Permission Example
WebElement notifications = driver.findElement(
By.id("allowNotifications")
);
if(!notifications.isSelected()){
notifications.click();
}
Assert.assertTrue(notifications.isSelected());
66. Practical Multi-Checkbox Example
List<WebElement> preferences = driver.findElements(
By.cssSelector("input.preference")
);
for(WebElement preference : preferences){
if(!preference.isSelected()){
preference.click();
}
}
for(WebElement preference : preferences){
Assert.assertTrue(preference.isSelected());
}
67. Checking Selected Option Text
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);
68. Checking Selected Option with isSelected()
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");
69. Multiple Select Dropdown
For a multi-select HTML dropdown, more than one option may be selected.
<select id="skills" multiple>
<option value="java">Java</option>
<option value="selenium">Selenium</option>
<option value="sql">SQL</option>
</select>
WebElement skills = driver.findElement(By.id("skills"));
Select select = new Select(skills);
select.selectByVisibleText("Java");
select.selectByVisibleText("Selenium");
List<WebElement> selected = select.getAllSelectedOptions();
for(WebElement option : selected){
System.out.println(
option.getText() + " : " + option.isSelected()
);
}
70. Deselect All Options
For a multi-select dropdown, Selenium's Select class provides deselectAll().
Select select = new Select(
driver.findElement(By.id("skills"))
);
select.deselectAll();
71. Verify Option Is Not Selected
WebElement javaOption = driver.findElement(
By.xpath("//select[@id='skills']/option[@value='java']")
);
Assert.assertFalse(javaOption.isSelected());
72. Common Mistake: Calling isSelected() on Every Element
isSelected() 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.
WebElement heading = driver.findElement(By.tagName("h1"));
System.out.println(heading.isSelected());
For normal non-selectable elements, the result is generally false.
73. Common Mistake: Confusing Selected and Enabled
A checkbox can be enabled without being selected.
WebElement checkbox = driver.findElement(By.id("subscribe"));
System.out.println("Enabled: " + checkbox.isEnabled());
System.out.println("Selected: " + checkbox.isSelected());
Do not use isEnabled() when the actual requirement is to verify selection.
74. Common Mistake: Clicking Without Checking Current State
Blindly clicking a checkbox can unintentionally toggle it from selected to unselected.
checkbox.click();
A safer approach when a specific final state is required is:
if(!checkbox.isSelected()){
checkbox.click();
}
75. Common Mistake: Using getAttribute("checked") as the Only Check
For selection-state validation, use isSelected() rather than relying only on the presence or value of the HTML checked attribute.
Assert.assertTrue(checkbox.isSelected());
76. Common Mistake: Using Thread.sleep()
A fixed delay should not be the primary mechanism for waiting for a checkbox or radio button to become selected.
Thread.sleep(3000);
Assert.assertTrue(checkbox.isSelected());
Prefer an explicit wait based on the actual state:
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementToBeSelected(By.id("terms"))
);
77. Best Practices for isSelected()
- Use isSelected() when validating selection state.
- Use it primarily with checkboxes, radio buttons, and select options.
- Check the current state before clicking a checkbox when a specific final state is required.
- Use explicit waits for dynamic selection changes.
- Use TestNG or JUnit assertions for expected selection states.
- Use Page Object Model methods to encapsulate selection logic.
- Use Select for HTML select elements.
- Use isEnabled() separately when enabled state matters.
- Use isDisplayed() separately when displayedness matters.
- Be careful with stale elements in dynamically rendered applications.
78. Advantages of isSelected()
- Simple boolean-based state check.
- Easy to use with Selenium WebElement.
- Useful for checkbox validation.
- Useful for radio button validation.
- Useful for HTML select options.
- Works well with TestNG and JUnit assertions.
- Can be combined with explicit waits.
- Useful for Page Object Model frameworks.
- Helps validate user selection workflows.
- Useful in real-world form and e-commerce automation.
79. Limitations and Considerations
- It does not select or deselect an element.
- It does not check whether an element is enabled.
- It does not check whether an element is displayed.
- It does not automatically wait for a state change.
- It is primarily meaningful for selectable controls.
- A dynamic DOM can make a stored WebElement stale.
- Application-specific visual states may require additional checks.
80. Quick Comparison of Selenium WebElement State Methods
| Method | Purpose | Typical Usage |
| isDisplayed() | Checks displayedness | Verify visibility of elements |
| isEnabled() | Checks enabled state | Verify whether controls are enabled |
| isSelected() | Checks selected state | Checkboxes, radio buttons, options |
| click() | Interacts with an element | Select checkbox/radio or activate controls |
| getAttribute() | Reads attribute/property-related value | Inspect HTML state information |
81. Interview Question: What is isSelected() in Selenium?
Answer: 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.
82. Interview Question: What does isSelected() return?
Answer: It returns true when the element is selected and false when it is not selected.
83. Interview Question: Does isSelected() select an element?
Answer: No. It only checks the current selection state. It does not perform the selection. For checkboxes and radio buttons, click() is normally used to change the state.
84. Interview Question: Which elements are commonly used with isSelected()?
Answer: The most common elements are checkboxes, radio buttons, and option elements inside HTML select controls.
85. Interview Question: What is the difference between isSelected() and isEnabled()?
Answer: isSelected() checks whether an element is selected, while isEnabled() checks whether an element is enabled for interaction.
86. Interview Question: What is the difference between isSelected() and isDisplayed()?
Answer: isSelected() checks selection state, while isDisplayed() checks whether the element is displayed according to WebDriver's displayedness semantics.
87. Interview Question: How do you select a checkbox only if it is not already selected?
WebElement checkbox = driver.findElement(By.id("terms"));
if(!checkbox.isSelected()){
checkbox.click();
}
88. Interview Question: How do you verify that a checkbox is selected?
WebElement checkbox = driver.findElement(By.id("terms"));
Assert.assertTrue(checkbox.isSelected());
89. Interview Question: How do you verify that a checkbox is not selected?
WebElement checkbox = driver.findElement(By.id("terms"));
Assert.assertFalse(checkbox.isSelected());
90. Interview Question: How do you wait until a checkbox is selected?
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementToBeSelected(By.id("terms"))
);
91. Interview Question: Can isSelected() be used with XPath?
Answer: Yes. Selenium can locate the element using XPath and then call isSelected().
WebElement checkbox = driver.findElement(
By.xpath("//input[@id='terms']")
);
boolean selected = checkbox.isSelected();
92. Interview Question: Can isSelected() be used with CSS Selector?
Answer: Yes.
WebElement checkbox = driver.findElement(
By.cssSelector("#terms")
);
boolean selected = checkbox.isSelected();
93. Interview Question: Can isSelected() be used with dropdown options?
Answer: Yes. It can be called on an individual HTML <option> element to check whether that option is selected.
WebElement option = driver.findElement(
By.xpath("//select[@id='country']/option[@value='india']")
);
Assert.assertTrue(option.isSelected());
94. Interview Question: How can you verify a selected dropdown option?
Select select = new Select(
driver.findElement(By.id("country"))
);
WebElement option = select.getFirstSelectedOption();
Assert.assertTrue(option.isSelected());
Assert.assertEquals(option.getText(), "India");
95. Interview Question: Can an element be enabled but not selected?
Answer: Yes. For example, a checkbox can be enabled and available for interaction while currently remaining unchecked.
System.out.println(checkbox.isEnabled());
System.out.println(checkbox.isSelected());
96. Interview Question: Can a selected element be disabled?
Answer: Yes. Selection state and enabled state are separate concepts. A control can retain a selected state while being disabled by the application.
97. Practical Project Workflow
A practical Selenium form automation project can use isSelected() to validate user preferences.
- Open the registration page.
- Enter the user's information.
- Locate the terms and conditions checkbox.
- Check whether the checkbox is already selected.
- Select it if necessary.
- Verify that it is selected.
- Select the required radio button.
- Verify the radio button state.
- Select the required dropdown option.
- Verify the selected option.
- Submit the form.
- Verify the resulting page.
98. Complete Practical Example
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");
99. isSelected() Automation Flow
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
100. Final Summary
isSelected() 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.
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 isSelected() and then perform click() only when necessary.
For dynamic applications, remember that isSelected() is an immediate state check. Use WebDriverWait and appropriate expected conditions when the selection state changes asynchronously. Also keep selection, visibility, and enabled state separate by using isSelected(), isDisplayed(), and isEnabled() according to the testing requirement.
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 JustAcademy Selenium Training.
For a practical introduction and course demonstration, visit Register for Selenium Course Demo.