Selenium
Python
drop-down menu
automation
web development

How to select a drop-down menu value with Selenium using Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Selenium, the correct way to select a value from a real HTML select element is the Select helper class. The main methods are select_by_visible_text, select_by_value, and select_by_index. The important caveat is that this only applies to actual select tags. Many modern web apps use custom dropdown widgets that need a different automation strategy.

Using Select for Real select Elements

If the page contains standard HTML like this:

html
1<select id="country">
2  <option value="ca">Canada</option>
3  <option value="us">United States</option>
4</select>

then Selenium's Select class is the right tool.

python
1from selenium import webdriver
2from selenium.webdriver.common.by import By
3from selenium.webdriver.support.ui import Select
4
5# assumes the page is already open
6
7driver = webdriver.Chrome()
8driver.get("https://example.com/form")
9
10country = Select(driver.find_element(By.ID, "country"))
11country.select_by_visible_text("Canada")
12
13print(country.first_selected_option.text)
14driver.quit()

You can also select by the underlying value attribute:

python
country.select_by_value("ca")

or by numeric position:

python
country.select_by_index(0)

Visible text is usually the clearest and least fragile when the option label is stable.

Wait for the Element Before Selecting

Dropdown tests often fail because the element is not ready yet, not because the selection code is wrong. Use WebDriverWait when the page loads data dynamically.

python
1from selenium import webdriver
2from selenium.webdriver.common.by import By
3from selenium.webdriver.support.ui import Select, WebDriverWait
4from selenium.webdriver.support import expected_conditions as EC
5
6
7driver = webdriver.Chrome()
8driver.get("https://example.com/form")
9
10select_element = WebDriverWait(driver, 10).until(
11    EC.element_to_be_clickable((By.ID, "country"))
12)
13Select(select_element).select_by_visible_text("Canada")
14
15driver.quit()

This avoids timing bugs where Selenium tries to interact before the dropdown is ready.

Custom Dropdowns Are Different

A lot of sites do not use a select tag at all. Instead they render a div, button, or list-based component that only looks like a dropdown.

In those cases, Select(...) will not work because Selenium only supports it for actual select elements.

The automation flow then becomes:

  1. click the widget to open it
  2. wait for the options panel
  3. click the desired option

Example pattern:

python
1from selenium import webdriver
2from selenium.webdriver.common.by import By
3from selenium.webdriver.support.ui import WebDriverWait
4from selenium.webdriver.support import expected_conditions as EC
5
6
7driver = webdriver.Chrome()
8driver.get("https://example.com/custom-dropdown")
9
10WebDriverWait(driver, 10).until(
11    EC.element_to_be_clickable((By.CSS_SELECTOR, ".dropdown-toggle"))
12).click()
13
14WebDriverWait(driver, 10).until(
15    EC.element_to_be_clickable((By.XPATH, "//li[normalize-space()='Canada']"))
16).click()
17
18driver.quit()

That is not a Selenium limitation. It reflects the fact that the page is not using a native form control.

Verifying the Selection

Do not stop at clicking. Assert that the state actually changed.

For a native select, you can inspect the selected option:

python
selected = country.first_selected_option.text
assert selected == "Canada"

For a custom widget, assert against the displayed text, hidden input value, or another stable DOM signal.

Multi-Select Menus Need Explicit Handling

If the underlying HTML control allows multiple selections, Selenium's Select object exposes extra methods such as select_by_visible_text multiple times and deselect_all. That matters because automation code written for a single-select dropdown may silently produce the wrong test logic when the form actually accepts several values at once.

Common Pitfalls

The most common mistake is trying to wrap a non-select element in Selenium's Select class.

Another mistake is using select_by_index on a dropdown whose option order changes between test runs. Text or value is often safer.

A third pitfall is skipping waits and blaming Selenium when the real problem is timing.

Summary

  • Use Selenium's Select class only for real HTML select elements.
  • Choose select_by_visible_text, select_by_value, or select_by_index based on what is most stable.
  • Add explicit waits before interacting with dynamic pages.
  • Custom dropdown widgets need ordinary click-and-wait automation, not Select.
  • Always verify the selected state after interaction.

Course illustration
Course illustration

All Rights Reserved.