How to Type Text in Selenium Python: Simple Guide
To type text in Selenium Python, first locate the input element using a locator like
find_element, then use the send_keys() method to enter the text. For example, element.send_keys('your text') types the given string into the selected input field.Syntax
Use driver.find_element(By.METHOD, 'locator') to find the input field, then call send_keys('text') on that element to type text.
- driver: Your Selenium WebDriver instance.
- By.METHOD: Locator strategy like
ID,NAME,CSS_SELECTOR, etc. - send_keys: Method to simulate typing text into the element.
python
from selenium import webdriver from selenium.webdriver.common.by import By # Locate element input_element = driver.find_element(By.ID, 'input-id') # Type text input_element.send_keys('Hello World')
Example
This example opens a browser, navigates to a sample page, finds a text input by its name, and types "Hello Selenium" into it.
python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time # Setup Chrome driver (adjust path as needed) options = Options() options.add_argument('--headless') # Run in background service = Service() driver = webdriver.Chrome(service=service, options=options) try: driver.get('https://www.w3schools.com/html/html_forms.asp') time.sleep(1) # Wait for page to load # Locate the input field for 'First name' input_field = driver.find_element(By.NAME, 'firstname') # Type text into the input field input_field.send_keys('Hello Selenium') # Pause to see the result if not headless time.sleep(2) finally: driver.quit()
Output
No visible output; browser opens, types text into input, then closes.
Common Pitfalls
- Not waiting for the page or element to load before typing causes
NoSuchElementException. - Using incorrect locators leads to failure to find the element.
- Trying to type into elements that are not input fields or are disabled will not work.
- For clearing existing text, use
element.clear()beforesend_keys().
python
from selenium.webdriver.common.by import By # Wrong way: typing without waiting or clearing input_field = driver.find_element(By.ID, 'username') input_field.send_keys('user') # May fail if element not ready # Right way: clear before typing input_field.clear() input_field.send_keys('user')
Quick Reference
| Action | Code Example | Description |
|---|---|---|
| Locate element | driver.find_element(By.ID, 'input-id') | Finds element by ID |
| Type text | element.send_keys('text') | Types given text into element |
| Clear text | element.clear() | Clears existing text in input |
| Wait for element | WebDriverWait(driver, 10).until(...) | Waits until element is ready |
Key Takeaways
Use driver.find_element with a proper locator to find the input field before typing.
Use send_keys() method on the element to type text.
Clear existing text with clear() if needed before typing.
Always ensure the element is loaded and interactable before typing.
Use explicit waits to avoid timing issues.