Last modified: Oct 22, 2024 By Alexander Williams

Python Selenium: Sending Keys (Text Input)

Sending keys is a crucial feature in Selenium for interacting with input fields, text areas, and other elements where users can enter text. It allows you to automate tasks like filling out forms, searching on websites, or simulating user input. In this guide, we'll explore how to use the send_keys method in Python Selenium for sending text to web elements.

Basic Usage of send_keys

The send_keys method is used to simulate typing into an input field. Once you locate an element, you can use this method to input text directly. Here's a simple example of sending text to a search bar:


from selenium import webdriver
from selenium.webdriver.common.by import By

# Initialize the WebDriver (e.g., ChromeDriver)
driver = webdriver.Chrome()

# Open a website
driver.get("https://www.example.com")

# Find a search input field by its ID and send text to it
search_box = driver.find_element(By.ID, "search-input")
search_box.send_keys("Selenium tutorial")

# Close the browser
driver.quit()

In this example, we find the input field using its ID, then send the text "Selenium tutorial" using the send_keys method. For more information on finding elements by ID, you can refer to our article on Finding Elements by ID.

Clearing Input Fields Before Sending Keys

Sometimes, input fields might already contain some text, and it's important to clear them before sending new input. You can use the clear() method to remove any existing content:


# Clear any existing text in the search box before sending new text
search_box.clear()
search_box.send_keys("Python Selenium guide")

This snippet first clears the content of the input field using clear() and then sends the new text "Python Selenium guide". This ensures that the input field doesn't contain any leftover text from previous interactions.

Using Special Keys with Selenium

Selenium allows you to simulate special keys like ENTER, TAB, or BACKSPACE using the Keys class. This is useful for submitting forms or moving between fields:


from selenium.webdriver.common.keys import Keys

# Send text and press ENTER key
search_box.send_keys("Selenium documentation" + Keys.ENTER)

In this example, we send the text "Selenium documentation" and press the ENTER key, which is equivalent to hitting the enter button on your keyboard. You can also use Keys.TAB to move to the next input field. To learn more about handling complex user interactions, see our article on Basic Python Selenium Architecture.

Sending Keys to Password Fields

The send_keys method can also be used for entering text into password fields. Here's an example of sending text to a password input:


# Find the password input field by its name and enter a password
password_field = driver.find_element(By.NAME, "password")
password_field.send_keys("securepassword123")

This script locates a password field using By.NAME and enters a password. You can find more on using the NAME attribute for locating elements in our guide on Finding Elements by Name.

Common Issues with send_keys

When using send_keys, you might encounter errors like ElementNotInteractableException. This typically occurs when the input field is not visible or is disabled. To resolve this, you can wait for the element to become visible using WebDriverWait:


from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wait until the input field is visible
search_box = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "search-input"))
)
search_box.send_keys("Waiting for visibility")

This code waits up to 10 seconds for the input field to become visible before sending the text. This approach can be helpful for dynamic websites where elements may take some time to load.

Conclusion

The send_keys method in Selenium is essential for automating text input on web pages. Whether you're filling out forms, entering login credentials, or simulating searches, understanding how to effectively use send_keys can streamline your automation scripts. To further enhance your Selenium skills, check out the official Selenium documentation. Mastering text input automation will enable you to handle a wide range of web interactions with ease.