Introduction
A Select Dropdown is one of the most commonly used form elements in web applications. It allows users to choose a single option from a predefined list of values. Dropdowns help reduce user input errors and provide a clean and organized way to display multiple choices.
In Selenium, HTML <select> elements are handled using the Select class, which provides convenient methods for selecting options by visible text, value, or index.
In this tutorial, you’ll learn how to handle Select Dropdowns using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.
What is a Select Dropdown?
A Select Dropdown is an HTML element that displays multiple options while allowing users to select one option at a time.
Common examples include:
Country Selection
State Selection
Language Preferences
Payment Methods
Job Categories
User Roles
Example HTML:
<select id="country">
<option>India</option>
<option>USA</option>
<option>Canada</option>
</select>
The Selenium Select class provides built-in methods for interacting with these dropdowns efficiently.
Why Automate Select Dropdowns?
Automating Select Dropdowns helps you:
Validate user selections.
Test form functionality.
Perform data-driven testing.
Verify business workflows.
Improve automation coverage.
Common Methods Used
| Method | Purpose |
|---|---|
| select_by_visible_text() | Select an option using its displayed text |
| select_by_value() | Select an option using its value attribute |
| select_by_index() | Select an option using its index position |
| first_selected_option | Retrieves the currently selected option |
| options | Retrieves all available options |
| deselect_all() | Removes all selected options (for multi-select dropdowns only) |
Example
The following example selects “Option 2” from the dropdown on The Internet Herokuapp webpage and verifies that it was selected successfully.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
# Topic: 26. Dropdown Handling - Select Dropdown
# Practice site: https://the-internet.herokuapp.com/dropdown
# Run: pytest -s 26_examples/test_01_select_dropdown.py
#
# The Select class wraps HTML select elements and provides methods to choose
# options by visible text, value, or index.
def test_select_dropdown():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/dropdown")
dropdown = Select(driver.find_element(By.ID, "dropdown"))
dropdown.select_by_visible_text("Option 2")
selected = dropdown.first_selected_option.text
assert selected == "Option 2"
finally:
driver.quit()
Output
Option 2
The dropdown successfully selects Option 2, and the assertion verifies that the correct option was selected.
Understanding the Code
Import the Required Classes
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
Imports:
webdriverfor launching and controlling the browser.Byfor locating web elements.Selectfor handling HTML<select>dropdown elements.
Create the WebDriver
driver = webdriver.Chrome()
Launches a new Chrome browser session.
Open the Practice Website
driver.get("https://the-internet.herokuapp.com/dropdown")
Opens The Internet Herokuapp webpage containing the Select Dropdown.
Locate the Dropdown Element
dropdown = Select(
driver.find_element(By.ID, "dropdown")
)
First, Selenium locates the <select> element using its ID.
The Select class then wraps the element and provides various methods for interacting with the dropdown options.
Select an Option Using Visible Text
dropdown.select_by_visible_text("Option 2")
This method selects the option whose displayed text exactly matches:
Option 2
Selecting options using visible text is one of the most commonly used approaches because it closely resembles real user interaction.
Retrieve the Selected Option
selected = dropdown.first_selected_option.text
The first_selected_option property retrieves the currently selected option from the dropdown.
Using .text returns its displayed value.
Verify the Selected Option
assert selected == "Option 2"
The assertion verifies that Selenium selected the correct option successfully.
Close the Browser
driver.quit()
Closes the browser and ends the WebDriver session.
This is a recommended practice to ensure that all browser instances are properly terminated after test execution.
Selecting Options Using Value
Some dropdowns use HTML value attributes.
Example:
dropdown.select_by_value("2")
This selects the option whose HTML value attribute matches:
<option value="2">Option 2</option>
Selecting Options Using Index
You can also select options using their index positions.
Example:
dropdown.select_by_index(2)
Index values begin from:
0
However, selecting options using visible text is generally more readable and maintainable.
Retrieving All Available Options
The options property returns all available dropdown options.
Example:
options = dropdown.options
for option in options:
print(option.text)
Output:
Please select an option
Option 1
Option 2
This is particularly useful for validating dropdown contents.
Practical Example
Suppose an e-commerce website contains a Country dropdown during checkout.
The automation script:
Selects India.
Completes the shipping details.
Verifies that the available payment methods are displayed correctly.
This validates both the dropdown functionality and the application’s business logic.
Automation Testing Example
Consider an online banking application.
The registration page contains dropdowns for:
Country
Account Type
Security Questions
The automation script:
Selects the required options.
Completes the registration process.
Verifies that the account was created successfully.
Dropdowns are frequently used in enterprise applications for collecting structured user input.
Real-World Example
Select Dropdowns are commonly used in:
Banking applications
E-commerce websites
CRM systems
Healthcare portals
HR management systems
Government websites
Enterprise web applications
They are particularly useful whenever users must select options from predefined lists.
Advantages of Automating Select Dropdowns
Simulates real user interactions.
Supports data-driven testing.
Improves automation coverage.
Validates business workflows.
Reduces manual testing effort.
Common Mistakes Beginners Make
Forgetting to Use the Select Class
The following will not work correctly for HTML <select> elements:
dropdown.click()
Instead, always use:
Select()
to interact with Select Dropdowns.
Using Incorrect Locators
Always prefer stable locators such as:
ID
Name
CSS Selector
Avoid fragile XPath expressions whenever possible.
Selecting the Wrong Option
Always verify that the selected option matches the expected value.
Example:
dropdown.first_selected_option.text
Ignoring Synchronization
Some dropdowns are loaded dynamically after API calls or page transitions.
Use Explicit Wait whenever necessary before interacting with them.
Best Practices
Use the
Selectclass for handling HTML<select>elements.Prefer
select_by_visible_text()whenever possible.Verify the selected option using
first_selected_option.Use stable locators such as ID and Name.
Use Explicit Wait for dynamically loaded dropdowns.
Validate dropdown contents whenever required.
Conclusion
Select Dropdowns are among the most frequently automated form elements in Selenium. The Select class provides simple and reliable methods for selecting options using visible text, values, or indexes. By following best practices such as using stable locators, proper synchronization, and validating selected values, you can create robust and maintainable Selenium automation scripts.
Frequently Asked Questions (FAQs)
Which class is used to handle Select Dropdowns?
Use:
Select()
from:
selenium.webdriver.support.ui
Which method is most commonly used for selecting an option?
The most commonly used method is:
select_by_visible_text()
How can I verify the selected option?
Use:
dropdown.first_selected_option.text
Can I retrieve all available dropdown options?
Yes.
Use:
dropdown.options
to retrieve all available options.
Are Select Dropdowns commonly automated in Selenium?
Yes.
They are widely used in registration forms, payment pages, checkout workflows, and enterprise web applications.
Key Takeaways
Select Dropdowns allow users to choose predefined values.
Selenium uses the
Selectclass for handling HTML<select>elements.Use
select_by_visible_text(),select_by_value(), orselect_by_index()for selecting options.Use
first_selected_optionto verify the selected value.Prefer stable locators such as ID and Name.
Use Explicit Wait for dynamically loaded dropdowns.
Proper synchronization and validation improve automation reliability.
