Selenium Cheatsheet
Forms and Inputs
Use this Selenium reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Text Inputs
from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys field = driver.find_element(By.NAME, "username") field.clear() # clear existing value field.send_keys("alice@example.com") # type text field.send_keys(Keys.TAB) # move to next field # Read current value print(field.get_attribute("value")) # "alice@example.com"
Setting value via JavaScript (bypasses React/Angular state)
driver.execute_script("arguments[0].value = arguments[1];", field, "alice") # Then fire the input event so frameworks pick it up driver.execute_script( "arguments[0].dispatchEvent(new Event('input', {bubbles:true}));", field )
Checkboxes
checkbox = driver.find_element(By.ID, "agree-terms") print(checkbox.is_selected()) # True / False if not checkbox.is_selected(): checkbox.click() # check it if checkbox.is_selected(): checkbox.click() # uncheck it
Dropdowns (Select)
from selenium.webdriver.support.ui import Select select = Select(driver.find_element(By.ID, "country")) # Select methods select.select_by_visible_text("United States") select.select_by_value("US") select.select_by_index(2) # 0-based index # Deselect (multi-select only) select.deselect_by_visible_text("Canada") select.deselect_by_value("CA") select.deselect_by_index(1) select.deselect_all() # Read state print(select.first_selected_option.text) # text of current selection print([o.text for o in select.all_selected_options]) print([o.text for o in select.options]) # all options print(select.is_multiple) # True if multi-select
Multi-Select
multi = Select(driver.find_element(By.ID, "skills")) assert multi.is_multiple multi.select_by_visible_text("Python") multi.select_by_visible_text("JavaScript") selected = [o.text for o in multi.all_selected_options]
Custom Dropdowns (Non-<select>)
Many modern UIs use <div> or <ul> dropdowns. Treat them as regular click/locator interactions:
# 1. Click the trigger to open driver.find_element(By.CSS_SELECTOR, ".dropdown-trigger").click() # 2. Wait for the options list to appear from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".dropdown-menu"))) # 3. Click the desired option driver.find_element(By.XPATH, "//ul[@class='dropdown-menu']/li[text()='Option B']").click()
File Upload
file_input = driver.find_element(By.CSS_SELECTOR, "input[type='file']") file_input.send_keys("/absolute/path/to/file.pdf") # Multiple files file_input.send_keys("/path/to/a.pdf\n/path/to/b.pdf")
File inputs must be visible in the DOM (they don't need to be visible on screen — remove
display:nonevia JS if needed).
# Make hidden file input interactable driver.execute_script("arguments[0].style.display = 'block';", file_input) file_input.send_keys("/path/to/file.png")
Date Pickers
Native <input type="date">
date_field = driver.find_element(By.CSS_SELECTOR, "input[type='date']") date_field.send_keys("2025-12-31") # YYYY-MM-DD format # Or via JS to bypass browser date-picker UI driver.execute_script("arguments[0].value = '2025-12-31';", date_field)
Custom Date Pickers
# Open the picker driver.find_element(By.CSS_SELECTOR, ".datepicker-input").click() # Navigate months until the target month is visible while driver.find_element(By.CSS_SELECTOR, ".datepicker-month").text != "December 2025": driver.find_element(By.CSS_SELECTOR, ".datepicker-next").click() # Click the day driver.find_element(By.XPATH, "//td[@data-day='31']").click()
Range / Slider
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys slider = driver.find_element(By.CSS_SELECTOR, "input[type='range']") # Arrow keys move the slider slider.click() for _ in range(5): slider.send_keys(Keys.ARROW_RIGHT) # Set value via JS driver.execute_script("arguments[0].value = arguments[1];", slider, "75") driver.execute_script( "arguments[0].dispatchEvent(new Event('change', {bubbles:true}));", slider )
Form Submission
# Via submit button driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click() # Via element.submit() (submits the enclosing <form>) driver.find_element(By.NAME, "email").submit() # Via Enter key driver.find_element(By.NAME, "search").send_keys(Keys.RETURN)
Read-Only and Disabled Fields
field = driver.find_element(By.ID, "generated-code") print(field.get_attribute("readonly")) # "true" or None print(field.get_attribute("disabled")) # "true" or None print(field.is_enabled()) # False when disabled # Read value of disabled/readonly field print(field.get_attribute("value")) # still readable
Gotchas
Selectonly works on<select>elements. Custom dropdowns require click-based interactions.
clear()may not fire React's syntheticonChange. Use the JS dispatch pattern orsend_keys(Keys.CONTROL + "a", Keys.DELETE)to trigger the event.
File upload paths must be absolute and point to a file that exists on the machine running the browser (not the test runner if using Grid).