HOWTO · Python

The send_keys() Method in Selenium Python

This tutorial demonstrates how to use the send_keys() method in Selenium Python.

On this page

send_keys() is a method on Selenium’s WebElement that sends text and keyboard input to a located element — usually a text box, form field, or any editable control. Once you have the element, element.send_keys("your text") types each character into it, and element.send_keys(Keys.ENTER) sends a named key such as Enter. It is the standard way to fill forms, run searches, and check how an application reacts to typed input.

What send_keys() Does in Selenium Python

send_keys() is defined on WebElement, so you always call it on an element you have already located, such as driver.find_element(By.NAME, "q").send_keys("selenium"). The method accepts one or more values — its signature is variadic, send_keys(*value) — and each value is a string of characters to type, sent in the order given. Special keys (Enter, Tab, Backspace, modifiers) live in selenium.webdriver.common.keys.Keys; each constant is just one character in the Unicode private-use area, so you can mix them into an ordinary string or pass them as separate arguments, as in element.send_keys("text", Keys.ENTER). element.send_keys("selenium" + Keys.ENTER) types the text and then presses Enter.

Two details matter in practice:

  • send_keys() simulates typing. It only enters input — the application’s own logic decides what happens next. Whether a login is rejected, a search runs, or a field shows a message is application behavior that your test observes, not something send_keys() performs.
  • The element must be able to receive input. Text fields, text areas, contenteditable elements, and file inputs (which take a file path as text) work; sending keys to a plain <div> delivers the events but usually changes nothing.

Set Up a Page and Locate an Element

Before you can send keys, start a driver, open the page, and find the target element. With from selenium import webdriver and from selenium.webdriver.common.by import By, the minimal setup is driver = webdriver.Chrome(), driver.get("https://www.google.com/"), then search_box = driver.find_element(By.NAME, "q"). Current Selenium (version 4) locates elements through driver.find_element(By.*, "..."); the older find_element_by_id, find_element_by_name, and similar methods were removed.

For pages that render fields dynamically, wait for the element instead of guessing: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "q"))) returns it once it is ready, using WebDriverWait from selenium.webdriver.support.ui and expected_conditions as EC from selenium.webdriver.support. A fixed time.sleep() before locating is brittle, because the test still fails if the element renders late.

Prefer the most stable locator available: an element ID first, then a name or CSS selector, and XPath for structure-based lookups. The browser snippets in this article assume pip install selenium plus an installed browser, with the driver resolved through Selenium Manager. They are written for Selenium 4 and are shown as inline syntax to adapt rather than executed here.

Send Text and Keyboard Keys

To fill a field, pass the text to send_keys(); to submit the form, append Keys.ENTER or send it in a second call (Keys lives in selenium.webdriver.common.keys). On the Google page located above, search_box.send_keys("This is filed by Selenium send keys") types the query and search_box.send_keys(Keys.ENTER) submits it. A modifier sent inside send_keys() — for example Keys.CONTROL + "a" — presses the keys together, but it stays depressed until you release it explicitly, either by sending the same modifier again or by sending Keys.NULL. Keys.chord(Keys.CONTROL, "a") is the form that lets modifiers go on its own, because it appends Keys.NULL after the chord.

The screenshot shows the text typed into Google’s search field.

Selenium send_keys() - Example 1

A Keys constant is a single character, so building the string yourself shows exactly what a chord adds. Run this example:

select_all = "\ue009" + "a"        # Keys.CONTROL + "a"
field_value = "selenium" + "\ue007"  # text + Keys.ENTER
print(len(select_all), len(field_value))
2 9

Each Keys constant counts as one character, so "selenium" + Keys.ENTER sends nine key events: eight letters plus Enter.

Validate Input and Verify Results

send_keys() performs no checks of its own — the application validates. To test validation, send a value, trigger the application, then read back what it shows. On a login form the sequence is email = driver.find_element(By.NAME, "email"), email.send_keys("not-an-email"), email.send_keys(Keys.ENTER), and then two common verifications: email.get_attribute("value") reads the field back — text inputs keep their content in value, not in the element’s .text — and driver.find_element(By.CSS_SELECTOR, ".error-message").text reads the response the application renders.

The same pattern covers the usual scenarios. For an email field, send a value that breaks the format and confirm the application refuses it; for a password field, send values that break each rule and confirm the matching message; for a search bar, send a query and check that the results the application returns are relevant. In every case send_keys() supplies the keystrokes, and your assertions confirm how the application responds.

Clear or Replace Text

To overwrite a field you have two options:

  • element.clear() empties an editable field with one WebDriver command.
  • Keyboard editing deletes or selects text one keystroke at a time: Keys.BACKSPACE removes the character before the caret, Keys.DELETE removes the one after it, and Keys.CONTROL + "a" selects everything so the next send_keys() call replaces it.

The distinction matters when a field reacts to each keystroke — autocompletes and live formatters may need real key events, while clear() fires a single update. On the Google search box the editing sequence is search_box.send_keys("For test purpose") to type a value, search_box.send_keys(3 * Keys.BACKSPACE) to delete its last three characters (leaving “For test purp”), search_box.clear() to empty the field, and search_box.send_keys("testing second key") to type fresh text. A time.sleep(3) between calls lets you watch each step in the browser, and driver.quit() closes it when done.

First step — the text is typed into the field:

Selenium send_keys() - Example 2 First Step

Second step — three characters are removed by Backspace, leaving “For test purp”:

Selenium send_keys() - Example 2 Second Step

Third step — clear() empties the field and new text is retyped:

Selenium send_keys() - Example 2 Third Step

Troubleshoot send_keys()

When input does not reach the element, work through these checks:

  • NoSuchElementException: the locator does not match, the page has not finished rendering, or the element sits inside an <iframe> or shadow DOM. Wait with WebDriverWait, and use driver.switch_to.frame(...) for framed content.
  • StaleElementReferenceException: the page re-rendered after you located the element, so locate it again right before typing.
  • ElementNotInteractableException: the element is hidden, disabled, or covered. Scroll it into view, wait for element_to_be_clickable, and check the disabled and readonly attributes — a read-only field rejects input.
  • InvalidElementStateException: the target cannot be edited, for example calling clear() on a non-editable element.
  • Nothing appears: focus may be elsewhere — click the element first, or drive it with ActionChains(driver).move_to_element(el).click().send_keys(text). Confirm what arrived by reading element.get_attribute("value").
  • Shortcuts and special keys: use Keys constants or Keys.chord(); send_keys() sends characters, not physical keystrokes, so it does not drive IME composition for scripts that need it.