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
.
- Selenium Features
- Selenium Installation
- Selenium Locators
- XPath Fundamentals
- CSS Selectors Methods
- Different Browsers Execution
- find_element & find_elements
- Check Enabled Status
- Check Displayed Status
- Check Selected Status
- Selenium Waits
- Send_keys Method
- Click Method
- Get Text
- Get Attribute Value
- Get Current URL
- Forward, Back, Refresh
- Take Screenshot
- Handle Browser Tabs
- Handle iframe
- Mouse Hover
- Context-Click
- Drag & Drop
- Handle Alerts
- Handle Dropdown
- Execute Javascript
- Scroll To element
- Headless Mode Execution
- Chrome Options
- Keyboard Action
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.