0
0
Selenium Pythontesting~5 mins

Select class for dropdowns in Selenium Python

Choose your learning style9 modes available
Introduction

The Select class helps you easily pick options from dropdown menus on web pages during testing.

When you need to choose a country from a dropdown list on a signup form.
When testing a web page where you select a product size from a dropdown.
When verifying that the correct option is selected after a user action.
When automating form filling that includes dropdown menus.
When checking that dropdown options are present and selectable.
Syntax
Selenium Python
from selenium.webdriver.support.ui import Select

select = Select(web_element)
select.select_by_visible_text('Option Text')
select.select_by_value('option_value')
select.select_by_index(index)

You must first find the dropdown element using a locator like find_element.

The Select class only works with <select> HTML elements.

Examples
Selects the option that shows 'Option 1' in the dropdown with ID 'dropdown_id'.
Selenium Python
select = Select(driver.find_element(By.ID, 'dropdown_id'))
select.select_by_visible_text('Option 1')
Selects the option with value attribute 'US' in the dropdown named 'country'.
Selenium Python
select = Select(driver.find_element(By.NAME, 'country'))
select.select_by_value('US')
Selects the third option (index starts at 0) in the dropdown with class 'city'.
Selenium Python
select = Select(driver.find_element(By.CSS_SELECTOR, 'select.city'))
select.select_by_index(2)
Sample Program

This script opens a simple HTML page with a dropdown of fruits. It selects 'Banana' by visible text and prints the selected option's text.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time

# Setup WebDriver (make sure driver executable is in PATH)
driver = webdriver.Chrome()

# Open a simple page with a dropdown
html = '''
<html>
<body>
  <select id="fruits">
    <option value="apple">Apple</option>
    <option value="banana">Banana</option>
    <option value="cherry">Cherry</option>
  </select>
</body>
</html>
'''

# Load the HTML content
file_path = 'file:///tmp/test_dropdown.html'
with open('/tmp/test_dropdown.html', 'w') as f:
    f.write(html)

# Open the local file
driver.get(file_path)

# Find the dropdown element
dropdown = driver.find_element(By.ID, 'fruits')

# Create Select object
select = Select(dropdown)

# Select by visible text
select.select_by_visible_text('Banana')

# Verify selection
selected_option = select.first_selected_option
print(f"Selected option text: {selected_option.text}")

# Cleanup
time.sleep(1)
driver.quit()
OutputSuccess
Important Notes

Always ensure the dropdown element is a <select> tag; otherwise, Select class won't work.

Use explicit waits if the dropdown loads dynamically to avoid errors.

Remember to close the browser after tests to free resources.

Summary

The Select class makes choosing dropdown options easy and readable.

You can select options by visible text, value, or index.

Works only with standard HTML <select> elements.