Common element interactions
Click, type, clear, select from dropdowns, upload files, and read text, attributes and state from the elements you find.
Typing, clicking and clearing
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
email = wait.until(EC.element_to_be_clickable((By.ID, "email")))
email.clear()
email.send_keys("[email protected]")
password = driver.find_element(By.CSS_SELECTOR, "input[type=password]")
password.send_keys("correct horse battery staple")
password.send_keys(Keys.ENTER)
# keyboard combinations on a focused element
search = driver.find_element(By.NAME, "q")
search.send_keys("selenium")
search.send_keys(Keys.CONTROL, "a")
search.send_keys(Keys.DELETE)send_keysappends; callclear()first or you will concatenate values across a retry.clear()on a field the page re-renders can race; a more robust reset is selecting all and deleting, or reloading the form.Keys.ENTERsubmits a form the way a keyboard user would, which is often closer to reality than clicking a styled button.element_to_be_clickablewaits for visibility and enabled state, which is the condition you almost always mean.
💡
If a click does nothing and no exception is raised, the usual cause is that the element moved between the hit test and the click. Waiting for
element_to_be_clickable and scrolling the element into view first removes most of these silent no-ops.Dropdowns, checkboxes and radios
from selenium.webdriver.support.ui import Select
country = Select(driver.find_element(By.ID, "country"))
country.select_by_visible_text("United Kingdom")
country.select_by_value("GB")
country.select_by_index(0)
print([o.text for o in country.options])
print(country.first_selected_option.text)
print(country.is_multiple)
# multi-select: select several, then clear them
tags = Select(driver.find_element(By.ID, "tags"))
tags.select_by_value("api")
tags.select_by_value("docs")
tags.deselect_all()terms = driver.find_element(By.ID, "terms")
if not terms.is_selected():
terms.click()
assert terms.is_selected()
plan = driver.find_element(By.CSS_SELECTOR, "input[name=plan][value=annual]")
plan.click()
assert plan.get_attribute("checked") is not None| Method | Returns | Note |
|---|---|---|
select_by_visible_text | Nothing | Matches the rendered label |
select_by_value | Nothing | Matches the value attribute |
select_by_index | Nothing | Zero-based, order-dependent |
options | List of elements | Includes the placeholder option |
is_multiple | Boolean | Whether deselection is possible |
A custom dropdown built from divs is not a <select> and Select will raise UnexpectedTagNameException. Click the trigger, then click the option element - and give the option a stable test id, because generated class names change between builds.
Reading values, uploads and state
field = driver.find_element(By.ID, "email")
field.get_attribute("value") # the current value, even if typed
field.get_attribute("placeholder") # any attribute, or None
field.text # rendered text (not the value)
field.is_displayed()
field.is_enabled()
field.get_property("validity") # a DOM property, not an attribute
assert "error" in field.get_attribute("class")
# file upload: the input must exist in the DOM, even if hidden
upload = driver.find_element(By.CSS_SELECTOR, "input[type=file]")
upload.send_keys("/absolute/path/to/invoice.pdf")- Use
get_attribute("value")for form state;.texton an input returns an empty string. - Attribute versus property matters:
get_attributereflects the DOM attribute, so a checkbox toggled by the user may not change it.is_selected()is the reliable check. - File upload requires an absolute path and does not work on a remote Grid node unless the file exists on that node - use a file detector or upload a shared volume.
- For disabled controls,
is_enabled()is better than checking for a class name that a designer may rename.
# remote grids: ship the file with the session
from selenium.webdriver.remote.file_detector import LocalFileDetector
driver.file_detector = LocalFileDetector()
driver.find_element(By.CSS_SELECTOR, "input[type=file]").send_keys("invoice.pdf")FAQ
Why does send_keys put the text in the wrong field?
Usually the field was re-rendered after you located it, so you typed into a detached copy while the visible element stayed empty. Re-locate the element immediately before typing, and prefer waiting on a condition that confirms the value landed.
How do I interact with a hidden file input?
Send the keys directly to the input element rather than clicking the visible button. Selenium can write to an input that is not displayed; clicking the styled overlay only opens the operating system's file dialog, which WebDriver cannot control.
Related
Locators and waits Alerts, frames and windows
Last refreshed 2026-09-18.