Interacting with forms, dropdowns, keyboard and mouse actions
Fill inputs, pick options with Select, handle checkboxes and radio buttons, and use ActionChains for hover, drag and keyboard shortcuts.
Once you can find an element, the next screening question is how you interact with it correctly. Typing into a field is easy; choosing from a dropdown, hovering a menu or pressing a keyboard shortcut trips up many candidates. These examples use Python with Selenium 4, and the Java API has the same names.
Text fields, checkboxes and radio buttons
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://example.com/signup")
email = driver.find_element(By.ID, "email")
email.clear()
email.send_keys("test@example.com")
terms = driver.find_element(By.NAME, "terms")
if not terms.is_selected():
terms.click()
driver.find_element(By.CSS_SELECTOR, "input[value='monthly']").click()
email.send_keys(Keys.TAB)
Call clear() before typing into a field that may hold a value, or you append to it. Check is_selected() before clicking a checkbox; clicking a checked box unchecks it. Radio buttons are picked by clicking the one with the value you want. Read back a value with get_attribute("value"); .text is empty for inputs.
Dropdowns with Select
from selenium.webdriver.support.ui import Select
country = Select(driver.find_element(By.ID, "country"))
country.select_by_visible_text("India")
country.select_by_value("IN")
country.select_by_index(2)
print(country.first_selected_option.text)
The Select helper works only on a real HTML select element. Many sites build custom dropdowns from divs; for those, click the trigger, wait for the list, then click the option by its text. Wrapping a div in Select raises an error about an unexpected tag name.
Mouse and keyboard with ActionChains
from selenium.webdriver import ActionChains
menu = driver.find_element(By.ID, "account-menu")
ActionChains(driver).move_to_element(menu).pause(0.5).click(
driver.find_element(By.LINK_TEXT, "Settings")).perform()
ActionChains(driver).key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform()
src = driver.find_element(By.ID, "card-1")
dst = driver.find_element(By.ID, "done-column")
ActionChains(driver).drag_and_drop(src, dst).perform()
Nothing happens until perform(). Hover menus need the move before the click, because the link is not visible until then. When an element is off screen, scroll to it first with driver.execute_script("arguments[0].scrollIntoView(true);", el), and only use JavaScript clicks as a last resort, since they bypass the checks a real user faces.
Common mistakes and a practice habit
- Typing into a pre-filled field without
clear(). - Using Select on a custom dropdown.
- Forgetting
perform()on an ActionChains. - Clicking an element hidden under a sticky header and getting "element click intercepted". Scroll, then click.
- Uploading files by clicking the button. Instead,
send_keys("/path/file.pdf")to the file input.
Practice habit. Find a public practice site with forms. Each day automate one page end to end: fill every input type, submit, and assert the success message. Keep a file of the snippets that worked; it becomes your interview cheat sheet.
More in Software testing