Keyboard Action in Python Selenium

Introduction to Keyboard Actions in Selenium

When automating web applications with Python Selenium, handling keyboard interactions is crucial for testing scenarios like filling forms, simulating keypresses, and navigating applications. Selenium provides robust functionalities to simulate user interactions with a keyboard using the ActionChains class and send_keys method.

Import Required Modules

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

Using send_keys to Simulate Typing

The send_keys method allows us to send keyboard inputs to elements like text fields.

Example: Entering Text in a Textbox

# Launch browser
driver = webdriver.Chrome()
driver.get("https://facebook.com")

# Locate the input field
input_field = driver.find_element("name", "username")

# Simulate typing
input_field.send_keys("TestUser")

Keyboard Shortcuts Using Keys Class

Selenium provides the Keys class to simulate keyboard shortcuts like CTRL+C, CTRL+V, ENTER, and more.

Example: Pressing ENTER Key

input_field.send_keys(Keys.ENTER)

Example: Copy-Paste Using Keyboard Shortcuts

input_field.send_keys("Test")
input_field.send_keys(Keys.CONTROL, 'a')  # Select all
input_field.send_keys(Keys.CONTROL, 'c')  # Copy
input_field.send_keys(Keys.TAB)  # Move to next field
input_field.send_keys(Keys.CONTROL, 'v')  # Paste

Using ActionChains for Complex Keyboard Actions

ActionChains helps perform multiple actions, including pressing and releasing keys.

Example: Using ActionChains for Keyboard Events

# Create ActionChains instance
actions = ActionChains(driver)

# Locate element
element = driver.find_element("id", "search")

# Perform actions
actions.click(element)
actions.send_keys("Selenium Automation")
actions.send_keys(Keys.RETURN)
actions.perform()

Handling Special Keys

Some keys like BACKSPACE, DELETE, ESC, F1-F12, SHIFT, ALT, CONTROL are essential in automation.

Example: Using Special Keys

input_field.send_keys(Keys.BACKSPACE)  # Deletes last character
input_field.send_keys(Keys.ESCAPE)  # Simulates ESC key

Pressing and Holding Keys

Pressing and holding keys like SHIFT allows us to capitalize text or perform shortcuts.

Example: Holding SHIFT Key

actions.key_down(Keys.SHIFT).send_keys("selenium").key_up(Keys.SHIFT).perform()

Using send_keys_to_element Method

Instead of sending keys directly, send_keys_to_element allows targeted input.

Example: Sending Keys to Specific Element

from selenium.webdriver.common.actions.action_builder import ActionBuilder

action = ActionBuilder(driver)
action.send_keys_to_element(input_field, "Automated Text").perform()

Automating Login Forms Using Keyboard Actions

Example: Automating Login with Keyboard Inputs

driver.get("https://facebook.com/login")

# Locate fields
username = driver.find_element("name", "username")
password = driver.find_element("name", "password")

# Type credentials
username.send_keys("testuser")
password.send_keys("securepassword")
password.send_keys(Keys.RETURN) # Press Enter

Handling Alerts and Popups Using Keyboard Keys

Example: Closing an Alert Using ESC

alert = driver.switch_to.alert
actions.send_keys(Keys.ESCAPE).perform()

Leave a Comment