Form Interactions & Dropdowns
Forms and dropdowns are in virtually every web application — login forms, registration pages, checkout flows, search filters. Selenium provides specialized utilities for handling native HTML select dropdowns, and you must know how to handle modern custom dropdowns, date pickers, checkboxes, and radio buttons that don't use standard HTML elements.
Form Element Interactions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ── TEXT INPUT ────────────────────────────────────────────────
email = driver.find_element(By.ID, "email")
email.clear()
email.send_keys("alice@example.com")
# ── NATIVE HTML SELECT DROPDOWN ───────────────────────────────
country_dropdown = driver.find_element(By.ID, "country-select")
select = Select(country_dropdown)
select.select_by_visible_text("United States") # By displayed text
select.select_by_value("US") # By HTML value attribute
select.select_by_index(2) # By position (0-indexed)
# Get all options
all_options = [opt.text for opt in select.options]
# Get selected option
current = select.first_selected_option.text
# Multi-select dropdown
multi = Select(driver.find_element(By.ID, "skills"))
multi.select_by_visible_text("Python")
multi.select_by_visible_text("Selenium")
# ── CHECKBOX ─────────────────────────────────────────────────
checkbox = driver.find_element(By.ID, "terms-checkbox")
if not checkbox.is_selected():
checkbox.click() # Only click if not already checked
# Verify checkbox is checked
assert checkbox.is_selected(), "Terms checkbox should be checked"
# ── RADIO BUTTONS ─────────────────────────────────────────────
# Select "Female" radio
female_radio = driver.find_element(By.CSS_SELECTOR, 'input[value="female"]')
female_radio.click()
# Verify selection
assert female_radio.is_selected()
# ── CUSTOM DROPDOWN (React/Angular — not native <select>) ──────
# Custom dropdowns are regular divs/spans, not <select> elements
# Example: Material UI dropdown
custom_dropdown = driver.find_element(By.CSS_SELECTOR, '[data-testid="country-dropdown"]')
custom_dropdown.click() # Open the dropdown
# Wait for options to appear
wait = WebDriverWait(driver, 10)
options = wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, '[data-testid="country-option"]')
))
# Find and click the desired option
for option in options:
if option.text == "Canada":
option.click()
breakCommon Mistakes
- Using Select class on custom dropdowns — the Select class ONLY works with native HTML <select> elements; custom dropdowns need click-based interaction
- Not clearing text fields before typing — send_keys appends to existing text; always .clear() first
- Clicking already-selected checkboxes — always check is_selected() before clicking to avoid toggling to the wrong state
- Hardcoding dropdown index — indices shift when options are reordered; use select_by_visible_text or select_by_value for stability
Tip
Tip
Practice Form Interactions Dropdowns in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Write test first → make it pass → clean up → repeat
Practice Task
Note
Practice Task — (1) Write a working example of Form Interactions Dropdowns from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Form Interactions Dropdowns is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready software testing code.
Key Takeaways
- Forms and dropdowns are in virtually every web application — login forms, registration pages, checkout flows, search filters.
- Using Select class on custom dropdowns — the Select class ONLY works with native HTML <select> elements; custom dropdowns need click-based interaction
- Not clearing text fields before typing — send_keys appends to existing text; always .clear() first
- Clicking already-selected checkboxes — always check is_selected() before clicking to avoid toggling to the wrong state