+91 88606 33966            edu_sales@siriam.in                   Job Opening : On-site Functional Trainer/Instructor | Supply Chain Management (SCM)
How to handles Drop down in selenium

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.

Tasks

The Selenium web form page

(https://www.selenium.dev/selenium/web/formPage.html) provides dropdown examples that we will use to demonstrate how to handle dropdowns using the Select class in Selenium with Python.

Scenario

On the provided web form page, there’s a dropdown <select> element with different options (like “Select an option”, “One”, “Two”, etc.). We will:

  • 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 <select> dropdown element
dropdown_element = driver.find_element(By.NAME, "selectomatic")

# Wrap the dropdown element in a Select object
dropdown = Select(dropdown_element)

So we have located the <select> dropdown using the name attribute and then wrapped the located <select> element in a Select object, providing access to dropdown-specific methods.

# Locate the <select> 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 <option> elements in the dropdown and loops through each option and prints its visible text.

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 <option> element and prints its visible text. And finally close the browser.

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 <select> 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

Similar Posts

How to Handle Alerts and Popups in Selenium

Locating Strategies in Selenium : Locating by Id

Installing Selenium and Configuring Web Driver

Selenium Python Example: How to run your first Test?

How to handles Drop down in selenium

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top