+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 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 element in a Select object, providing access to dropdown-specific methods.

# 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

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