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:
then Selenium's Select class is the right tool.
You can also select by the underlying value attribute:
or by numeric position:
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.
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:
- click the widget to open it
- wait for the options panel
- click the desired option
Example pattern:
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:
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
Selectclass only for real HTMLselectelements. - Choose
select_by_visible_text,select_by_value, orselect_by_indexbased 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.

