This article demonstrates how to handle dropdown menus using the Select class in Selenium with Python. By interacting with a dropdown on the Selenium web form page, we perform operations such as selecting options by visible text, value, and index, retrieving the currently selected option, and printing all available options.
Select options from the dropdown using various methods of the Select class.
Print all options.
Retrieve the selected option.
1. Start by importing the Libraries.
Here we have imported the Select library.
2. Create the driver instance and navigate to the specific url
# Initialize WebDriver
driver = webdriver.Chrome()
try:
# Navigate to the Selenium web form page
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
3. To locate the drop-down element by name.
# Locate the
So we have located the
# Locate the dropdown element
dropdown_element = driver.find_element(By.NAME, "selectomatic")
# Wrap the dropdown element in a Select object
dropdown = Select(dropdown_element)
4. Handling the drop-down using the visible-text method.
# Select an option by visible text
dropdown.select_by_visible_text("One")
print("Selected option by visible text:", dropdown.first_selected_option.text)
Here we have selected the option with the visible text “One” and then Retrieve and print the currently selected option’s text.
5. Handling the drop-down using the value method.
# Select an option by value
dropdown.select_by_value("two")
print("Selected option by value:", dropdown.first_selected_option.text)
Similarly, it selects the option where the value attribute is “two” and prints the selected option’s text.
6. Handling the drop-down using the index method.
# Select an option by index
dropdown.select_by_index(3) # Index 3 corresponds to the "Four" option
print("Selected option by index:", dropdown.first_selected_option.text)
7. Lists all options from the Drop-down menu.
# Print all options available in the dropdown
print("All options in the dropdown:")
for option in dropdown.options:
print(option.text)
The dropdown.options returns a list of all
8. Retrieve the Currently Selected Option
# Retrieve the currently selected option
selected_option = dropdown.first_selected_option
print("Currently selected option:", selected_option.text)
The first_selected_option retrieves the currently selected
Final Code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
# Initialize WebDriver
driver = webdriver.Chrome()
try:
# Navigate to the Selenium web form page
driver.get("https://www.selenium.dev/selenium/web/formPage.html")
# Locate the dropdown element
dropdown_element = driver.find_element(By.NAME, "selectomatic")
# Wrap the dropdown element in a Select object
dropdown = Select(dropdown_element)
# Select an option by visible text
dropdown.select_by_visible_text("One")
print("Selected option by visible text:", dropdown.first_selected_option.text)
# Select an option by value
dropdown.select_by_value("two")
print("Selected option by value:", dropdown.first_selected_option.text)
# Select an option by index
dropdown.select_by_index(0) # Index 0 corresponds to the "First" option
print("Selected option by index:", dropdown.first_selected_option.text)
# Print all options available in the dropdown
print("All options in the dropdown:")
for option in dropdown.options:
print(option.text)
# Retrieve the currently selected option
selected_option = dropdown.first_selected_option
print("Currently selected option:", selected_option.text)
finally:
# Quit the browser
driver.quit()
Output
With this you have understand how to handle drop-down elements in selenium