How to Select Dropdown in Selenium Python: Simple Guide
To select a dropdown option in Selenium Python, use the
Select class from selenium.webdriver.support.ui. First, locate the dropdown element, then create a Select object and use methods like select_by_visible_text(), select_by_value(), or select_by_index() to choose an option.Syntax
Use the Select class to interact with dropdown menus. First, import Select, then find the dropdown element by a locator. Create a Select object with this element. Use one of these methods to select an option:
select_by_visible_text(text): Selects option by the text shown.select_by_value(value): Selects option by the option's value attribute.select_by_index(index): Selects option by its position (starting at 0).
python
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select dropdown_element = driver.find_element(By.ID, 'dropdown_id') select = Select(dropdown_element) # Select by visible text select.select_by_visible_text('Option Text') # Select by value attribute select.select_by_value('option_value') # Select by index select.select_by_index(2)
Example
This example opens a webpage with a dropdown, selects an option by visible text, and prints confirmation. It shows how to use Selenium's Select class properly.
python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time # Setup Chrome driver (adjust path to your chromedriver) options = Options() options.add_argument('--headless') # Run without opening browser window service = Service(executable_path='chromedriver') driver = webdriver.Chrome(service=service, options=options) try: driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select') driver.switch_to.frame('iframeResult') # Switch to iframe containing dropdown dropdown_element = driver.find_element(By.TAG_NAME, 'select') select = Select(dropdown_element) select.select_by_visible_text('Saab') selected_option = select.first_selected_option.text print(f'Selected option: {selected_option}') finally: driver.quit()
Output
Selected option: Saab
Common Pitfalls
Common mistakes when selecting dropdowns include:
- Not importing
Selectclass. - Trying to select options on elements that are not
<select>tags. - Not switching to the correct iframe if the dropdown is inside one.
- Using incorrect locators that do not find the dropdown element.
- Using
select_by_visible_textwith text that does not exactly match the option.
Always verify the dropdown is a <select> element and check the exact text or value attributes.
python
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select # Wrong: element is not a select tag wrong_element = driver.find_element(By.ID, 'not_a_select') # This will raise UnexpectedTagNameException # select = Select(wrong_element) # Right: dropdown_element = driver.find_element(By.ID, 'correct_select') select = Select(dropdown_element) select.select_by_visible_text('Exact Option Text')
Quick Reference
Here is a quick summary of the main methods to select dropdown options:
| Method | Description | Example Usage |
|---|---|---|
| select_by_visible_text(text) | Select option by the text shown to users | select.select_by_visible_text('Option 1') |
| select_by_value(value) | Select option by the value attribute | select.select_by_value('val1') |
| select_by_index(index) | Select option by its zero-based index | select.select_by_index(0) |
| first_selected_option | Get currently selected option element | select.first_selected_option.text |
Key Takeaways
Use Selenium's Select class to interact with dropdowns that use
Select options by visible text, value attribute, or index depending on your need.
Always ensure the dropdown element is correctly located and is a
Switch to the correct iframe if the dropdown is inside one before selecting.
Avoid common errors like wrong locators or mismatched option text.