Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How to handle a drop-down field and select a value from it using Selenium?

To select a value from a drop-down field using Selenium, use the Select class from the `selenium.webdriver.support.ui` module. Here's an example of the same:


from selenium import webdriver
from selenium.webdriver.support.ui import Select

# create a new Chrome browser instance
driver = webdriver.Chrome()

# navigate to a web page with a dropdown field
driver.get("https://www.lambdatest.com")

# find the dropdown element by its ID
dropdown = Select(driver.find_element_by_id("dropdown-id"))

# select an option by visible text
dropdown.select_by_visible_text("Option 1")

# select an option by value
dropdown.select_by_value("option1")

# select an option by index
dropdown.select_by_index(0)

# close the browser
driver.quit()

To test a dropdown field in a website, you can create a new Chrome browser instance using `webdriver.Chrome()`. Then navigate to the website, find the dropdown field by its ID using `driver.find_element_by_id()`, and finally click on it.

To select an option from a dropdown, we create a Select object by passing it to the dropdown element to select an option from a dropdown. We can then choose an option from the dropdown by calling one of the following methods on the Select object: `select_by_index(i)` or `select_by_label(s)`.

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free