Introduction
Modern web applications support various keyboard shortcuts that allow users to perform actions quickly without using the mouse. Examples include selecting all text, copying, pasting, cutting, undoing, and saving.
Selenium can simulate keyboard shortcuts using the Keys class from selenium.webdriver.common.keys. Modifier keys such as Control, Shift, Alt, and Command can be combined with normal keys to imitate real keyboard behavior.
In this tutorial, you’ll learn how to automate keyboard shortcuts using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.
What are Keyboard Shortcuts?
A Keyboard Shortcut is a combination of one or more keys that performs a specific action.
Example:
CTRL + A
│
▼
Select All Text
Common keyboard shortcuts include:
Ctrl + A → Select All
Ctrl + C → Copy
Ctrl + V → Paste
Ctrl + X → Cut
Ctrl + Z → Undo
Why Automate Keyboard Shortcuts?
Keyboard shortcut automation helps you:
Test text editing.
Verify input controls.
Simulate real user behavior.
Test productivity features.
Automate applications that rely heavily on keyboard input.
How Selenium Performs Keyboard Shortcuts
Selenium uses the Keys class.
Example:
element.send_keys(
Keys.CONTROL,
"a"
)
This simulates pressing Ctrl + A.
Example
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Topic: 34. Keyboard Actions - Keyboard Shortcuts
# Practice site: https://the-internet.herokuapp.com/inputs
# Run: pytest -s 34_examples/test_01_keyboard_shortcuts.py
#
# Keyboard shortcuts combine modifier keys with normal keys. CONTROL + A selects
# all text in an input on Windows/Linux.
def test_keyboard_shortcut_select_all_and_replace():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/inputs")
number_input = driver.find_element(By.TAG_NAME, "input")
number_input.send_keys("123")
number_input.send_keys(Keys.CONTROL, "a")
number_input.send_keys("999")
assert number_input.get_attribute("value") == "999"
finally:
driver.quit()
Understanding the Code
Import Required Libraries
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
These modules are required to launch the browser, locate web elements, and simulate keyboard input.
Create a Chrome Browser Instance
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Practice Website
driver.get("https://the-internet.herokuapp.com/inputs")
Navigates to the practice page containing a numeric input field.
Locate the Input Field
number_input = driver.find_element(
By.TAG_NAME,
"input"
)
Locates the input element where keyboard actions will be performed.
Enter Initial Text
number_input.send_keys("123")
Types 123 into the input field.
Perform the Keyboard Shortcut
number_input.send_keys(
Keys.CONTROL,
"a"
)
Simulates pressing Ctrl + A, which selects all the existing text inside the input field.
Note: On macOS, the equivalent shortcut is typically Command + A (
Keys.COMMAND).
Replace the Selected Text
number_input.send_keys("999")
Since all the previous text is selected, typing 999 replaces the selected content.
The input now contains only 999.
Verify the Result
assert number_input.get_attribute(
"value"
) == "999"
Verifies that the input field contains the expected value after the keyboard shortcut operation.
If the value is different, the test fails.
Close the Browser
driver.quit()
Closes the browser and ends the WebDriver session.
Practical Example
Suppose an online spreadsheet allows users to edit cell values.
The automation script:
Enters a value.
Uses Ctrl + A to select it.
Types a new value.
Verifies that the old value has been replaced.
Automation Testing Example
Consider an online form where users frequently replace existing values.
The automation script:
Enters text into a field.
Uses keyboard shortcuts to select all text.
Replaces the existing content.
Verifies the updated value.
Real-World Example
Keyboard shortcuts are commonly used in:
Online document editors
Spreadsheet applications
Form-based web applications
IDEs and code editors
Email clients
Content Management Systems (CMS)
Enterprise web applications
Examples include selecting, copying, pasting, cutting, undoing, and editing text.
Advantages of Keyboard Shortcut Automation
Simulates real user behavior.
Tests productivity features.
Reduces dependence on mouse interactions.
Verifies text editing functionality.
Improves automation coverage.
Common Mistakes Beginners Make
Forgetting to Focus on the Input Element
Keyboard shortcuts work only when the correct element has focus.
Always locate and interact with the target element first.
Using the Wrong Modifier Key
Windows and Linux typically use Ctrl, while macOS generally uses Command.
Choose the appropriate modifier key for your environment.
Assuming the Shortcut Works Everywhere
Some applications implement custom keyboard shortcuts that may behave differently from standard browser shortcuts.
Always verify the application’s expected behavior.
Not Verifying the Final Value
Always check that the keyboard shortcut produced the expected result.
Best Practices
Use the
Keysclass for keyboard shortcuts.Ensure the correct element has focus before sending keys.
Verify the application’s response after the shortcut.
Use platform-appropriate modifier keys when necessary.
Keep keyboard interaction tests simple and readable.
Conclusion
Keyboard shortcuts are an important part of modern web applications and significantly improve user productivity. Selenium makes it easy to simulate these shortcuts using the Keys class. Understanding keyboard shortcut automation is essential for testing forms, editors, spreadsheets, enterprise applications, and other keyboard-driven interfaces.
Frequently Asked Questions (FAQs)
What are Keyboard Shortcuts?
Keyboard shortcuts are combinations of keys that perform specific actions, such as selecting all text, copying, or pasting.
Which Selenium class is used for keyboard shortcuts?
The Keys class is used to simulate keyboard keys and modifier combinations.
How do you simulate Ctrl + A?
Use:
element.send_keys(
Keys.CONTROL,
"a"
)
to simulate Ctrl + A.
Can Selenium automate keyboard shortcuts?
Yes. Selenium supports keyboard shortcuts using the send_keys() method together with the Keys class.
Where are keyboard shortcuts commonly used?
They are commonly used in document editors, spreadsheets, forms, IDEs, email clients, CMS platforms, and enterprise web applications.
Key Takeaways
Keyboard shortcuts simulate real keyboard interactions.
Selenium uses the
Keysclass to send special keys and modifier combinations.Keys.CONTROLcan be combined with other keys such as"a"to perform shortcuts like Ctrl + A.Always ensure the correct element has focus before sending keyboard shortcuts.
Verify the application’s response after performing the shortcut.
Keyboard shortcut automation is an essential Selenium skill for testing modern web applications.
