send_keys Method

The send_keys() method in Selenium is used to simulate typing text into input fields or elements such as text boxes, text areas, or other editable elements in web pages. It can also be used to send special keys like Enter, Tab, or Arrow keys by using the Keys class from selenium.webdriver.common.keys.

Syntax:

element.send_keys("text to input")

Example 1: Sending text to an input field

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

driver = webdriver.Chrome()
driver.get('https://www.google.co.in')

# Locate the input field
input_element = driver.find_element(By.NAME, 'q')

# Send text to the input field
input_element.send_keys("Python Selenium")

# Close the browser
driver.quit()

Example 2: Using special keys (e.g., Enter key)

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

driver = webdriver.Chrome()
driver.get('https://www.google.co.in')

# Locate the input field
input_element = driver.find_element(By.NAME, 'q')

# Send text and press Enter
input_element.send_keys("search query", Keys.ENTER)

# Close the browser
driver.quit()

Common Use Cases:

  • Filling forms by sending text to multiple input fields.
  • Simulating keyboard actions such as pressing Enter, Tab, or navigating using arrow keys.