Introduction
The is_selected() method is one of Selenium’s most useful WebElement methods for determining whether selectable elements such as checkboxes, radio buttons, and certain options within dropdown menus are currently selected. Automation engineers frequently use this method to validate user selections before performing actions or assertions during test execution.
Modern web applications often rely on selectable UI components for user preferences, form submissions, permissions, and settings management. The is_selected() method helps ensure that Selenium accurately validates an element’s current selection state, thereby improving automation reliability and reducing unexpected test failures.
In this tutorial, you’ll learn what is_selected() is, why it is used, its syntax, practical examples, real-world use cases, common mistakes, and best practices.
What is is_selected()?
The is_selected() method checks whether a selectable web element is currently selected.
If the element is selected, Selenium returns:
True
If the element is not selected, Selenium returns:
False
For example, consider the following HTML:
<input
type="checkbox">
<input
type="checkbox"
checked>
<input
type="radio"
checked>
Selenium can verify whether these elements are selected using:
element.is_selected()
The is_selected() method is commonly used for validating:
Checkboxes
Radio buttons
Dropdown options
User preferences
Form selections
Dynamic UI components
Why Use is_selected()?
The is_selected() method is useful because it:
Verifies whether selectable elements are selected.
Improves automation reliability.
Prevents unnecessary user interactions.
Supports UI validation.
Produces readable automation scripts.
Is extensively used in professional automation frameworks.
Syntax
element.is_selected()
Where:
element→ Previously located WebElement.is_selected()→ ReturnsTrueorFalsebased on the element’s selection state.
Example
The Selenium practice website contains two checkboxes. Selenium verifies that the first checkbox is not selected and that the second checkbox is selected by default.
The Selenium code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 18. Element State Verification - is_selected()
# Practice site: https://the-internet.herokuapp.com/checkboxes
# Run: pytest -s 18_examples/test_03_is_selected.py
#
# is_selected() checks whether a checkbox or radio button is currently selected.
def test_is_selected():
driver = webdriver.Chrome()
try:
driver.get(
"https://the-internet.herokuapp.com/checkboxes"
)
checkboxes = driver.find_elements(
By.CSS_SELECTOR,
"input[type='checkbox']"
)
assert checkboxes[0].is_selected() is False
assert checkboxes[1].is_selected() is True
finally:
driver.quit()
Output
The first checkbox is not
selected, while the second
checkbox is selected
successfully as expected.
Understanding the Code
Import the By Class
from selenium.webdriver.common.by import By
Imports Selenium’s locator strategies.
Open the Practice Website
driver.get(
"https://the-internet.herokuapp.com/checkboxes"
)
Launches the Selenium practice website.
Locate the Checkboxes
driver.find_elements(
By.CSS_SELECTOR,
"input[type='checkbox']"
)
Locates both checkboxes displayed on the webpage.
Verify the Selection States
checkboxes[0].is_selected()
checkboxes[1].is_selected()
These statements return:
False
True
which represent the current selection state of the respective checkboxes.
Validate the Result
assert checkboxes[0].is_selected() is False
assert checkboxes[1].is_selected() is True
Verifies that Selenium successfully validates the selection states of both elements.
How is_selected() Works
Python Script
│
▼
Locate Web Element
│
▼
is_selected()
│
▼
Check Selection State
│
▼
Return Result
True/False
│
▼
Perform Validation
Practical Example
Suppose you’re automating a Registration page.
The application contains:
Accept Terms and Conditions checkbox.
Receive Newsletter checkbox.
Gender selection radio buttons.
Before submitting the form, Selenium verifies:
checkbox.is_selected()
to ensure that the required options have been selected successfully.
This significantly improves automation reliability during form validation testing.
Automation Testing Example
Consider an online banking application.
Automation engineers frequently validate whether the following elements are selected:
Terms and Conditions checkboxes.
Account preference selections.
Transaction options.
Notification preferences.
Security settings.
User consent forms.
Dynamic UI components.
Professional automation frameworks extensively use is_selected() during UI regression testing.
Real-World Example
Automation engineers frequently use is_selected() while automating:
Banking applications.
E-Commerce websites.
Healthcare portals.
CRM systems.
ERP applications.
SaaS products.
Enterprise web applications.
Registration forms.
Dynamic dashboards.
Selection state validation is particularly important because modern web applications frequently update user preferences dynamically using JavaScript.
Advantages of is_selected()
Improves automation reliability.
Prevents unnecessary interactions.
Supports UI validation.
Produces maintainable automation scripts.
Works across browsers.
Extensively used in professional automation frameworks.
Common Mistakes Beginners Make
Clicking an Already Selected Checkbox
Avoid
checkbox.click()
without checking its current state.
Prefer
if not checkbox.is_selected():
checkbox.click()
Selection state validation significantly improves automation reliability.
Confusing is_selected() with is_enabled()
Remember that:
element.is_selected()
checks whether an element is:
Selected
whereas:
element.is_enabled()
checks whether an element is:
Enabled
Both methods serve different purposes and are frequently used together.
Ignoring Dynamic Webpages
Modern applications frequently:
Select elements dynamically.
Update form states asynchronously.
Modify user preferences using JavaScript.
Always use appropriate synchronization techniques before validating selection states.
Best Practices
Verify whether selectable elements are selected before performing actions.
Combine
is_selected()withis_displayed()andis_enabled()whenever appropriate.Use synchronization techniques for dynamically updated webpages.
Keep assertions simple and readable.
Validate form selections before submitting data.
Avoid performing unnecessary click operations.
Conclusion
The is_selected() method provides a simple and reliable mechanism for validating whether selectable web elements are currently selected during Selenium automation testing. It plays an important role in improving automation reliability, validating user interactions, and preventing unnecessary operations on already selected elements.
Understanding how and when to use is_selected() correctly is essential for building scalable, reliable, and maintainable Selenium automation frameworks used in professional software testing environments.
Frequently Asked Questions (FAQs)
What is is_selected() in Selenium?
The is_selected() method checks whether a selectable web element is currently selected.
What is the syntax of is_selected()?
element.is_selected()
What does is_selected() return?
It returns:
True
if the element is selected and:
False
if the element is not selected.
Which elements support is_selected()?
It is commonly used with:
Checkboxes
Radio buttons
Selectable dropdown options
When should I use is_selected()?
Use it when:
Validating user selections.
Testing forms and preferences.
Avoiding unnecessary click operations.
Verifying dynamically selected UI components.
Key Takeaways
is_selected()verifies whether a selectable web element is currently selected.It returns
Truefor selected elements andFalsefor unselected elements.It is commonly used with checkboxes and radio buttons.
Use it before performing click operations whenever appropriate.
Combine it with other WebElement validation methods for reliable automation.
The
is_selected()method is extensively used in professional Selenium automation frameworks.
