Special Keys

Introduction

Besides typing letters and numbers, web applications often require users to press special keyboard keys such as Enter, Tab, Escape, Backspace, and the Arrow keys.

Selenium provides support for these keys through the Keys class in selenium.webdriver.common.keys. These special keys allow automation scripts to simulate realistic keyboard interactions without using the physical keyboard.

In this tutorial, you’ll learn how to automate special key presses using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.


What are Special Keys?

Special Keys are non-character keyboard keys that perform specific actions.

Common examples include:

  • Enter

  • Tab

  • Escape

  • Backspace

  • Delete

  • Arrow Keys

  • Home

  • End

  • Page Up

  • Page Down

  • Function Keys

Example:

Press ESCAPE

      │

      ▼

Application Receives ESCAPE Event

Why Automate Special Keys?

Special key automation helps you:

  • Test keyboard navigation.

  • Verify shortcut functionality.

  • Close dialogs and popups.

  • Navigate forms.

  • Simulate real user interactions.


How Selenium Sends Special Keys

Selenium uses the Keys class.

Example:

element.send_keys(
    Keys.ESCAPE
)

This simulates pressing the Escape key.


Example

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


# Topic: 34. Keyboard Actions - Special Keys
# Practice site: https://the-internet.herokuapp.com/key_presses
# Run: pytest -s 34_examples/test_03_special_keys.py
#
# Selenium's Keys class contains special keys such as ENTER, TAB, ESCAPE,
# BACKSPACE, ARROW_DOWN, and many more.


def test_send_special_key_escape():
    driver = webdriver.Chrome()

    try:
        driver.get("https://the-internet.herokuapp.com/key_presses")

        driver.find_element(By.TAG_NAME, "body").send_keys(Keys.ESCAPE)

        result = driver.find_element(By.ID, "result").text
        assert "ESCAPE" in result
    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 send special keyboard keys.


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/key_presses")

Navigates to the Key Presses practice page.


Send the Escape Key

driver.find_element(
    By.TAG_NAME,
    "body"
).send_keys(Keys.ESCAPE)

Locates the page body and sends the Escape key.

The application detects the key press and displays which key was received.


Read the Result

result = driver.find_element(
    By.ID,
    "result"
).text

Locates the result element that displays the name of the key pressed.


Verify the Special Key

assert "ESCAPE" in result

Verifies that the application correctly detected the Escape key.

If the displayed text does not contain ESCAPE, the test fails.


Close the Browser

driver.quit()

Closes the browser and ends the WebDriver session.


Commonly Used Special Keys

The Keys class provides many special keyboard keys, including:

Key Selenium Constant
Enter Keys.ENTER
Return Keys.RETURN
Tab Keys.TAB
Escape Keys.ESCAPE
Backspace Keys.BACKSPACE
Delete Keys.DELETE
Space Keys.SPACE
Up Arrow Keys.ARROW_UP
Down Arrow Keys.ARROW_DOWN
Left Arrow Keys.ARROW_LEFT
Right Arrow Keys.ARROW_RIGHT
Home Keys.HOME
End Keys.END
Page Up Keys.PAGE_UP
Page Down Keys.PAGE_DOWN
Shift Keys.SHIFT
Control Keys.CONTROL
Alt Keys.ALT

Practical Example

Suppose an application closes a popup dialog when the user presses the Escape key.

The automation script:

  • Opens the popup.

  • Sends the Escape key.

  • Verifies that the popup closes.


Automation Testing Example

Consider a searchable dropdown.

The automation script:

  • Opens the dropdown.

  • Uses the Arrow Down key to navigate.

  • Presses Enter to select an item.

  • Verifies that the correct option is selected.


Real-World Example

Special keys are commonly used in:

  • Login forms

  • Search boxes

  • Dialog windows

  • Dropdown lists

  • Data entry forms

  • Spreadsheet applications

  • Enterprise web applications

Examples include submitting forms with Enter, navigating fields with Tab, closing dialogs with Escape, and moving through menus using the arrow keys.


Advantages of Special Key Automation

  • Simulates realistic keyboard interactions.

  • Tests accessibility features.

  • Verifies keyboard navigation.

  • Supports shortcut testing.

  • Improves automation coverage.


Common Mistakes Beginners Make

Sending Keys to the Wrong Element

Special keys are sent to the element that currently has focus.

Ensure the correct element is selected before calling send_keys().


Using Regular Strings Instead of Keys

Special keys must be sent using constants from the Keys class.

For example:

Keys.ENTER

instead of "ENTER".


Ignoring Application Focus

Some applications require a specific input field or the page body to have focus before special keys will work.


Not Verifying the Result

Always confirm that the application responded correctly after the key press.


Best Practices

  • Use the Keys class for all special keyboard keys.

  • Ensure the correct element has focus before sending keys.

  • Verify the application’s response after the key press.

  • Use special keys only when required by the application’s workflow.

  • Keep keyboard interaction tests simple and readable.


Conclusion

Special keys are an essential part of keyboard automation in Selenium. The Keys class allows automation scripts to simulate important keyboard actions such as Enter, Tab, Escape, Arrow keys, and many others. Understanding how to use special keys helps create realistic and reliable automation tests for forms, dialogs, menus, and modern web applications.


Frequently Asked Questions (FAQs)

What are Special Keys?

Special keys are non-character keyboard keys such as Enter, Escape, Tab, Backspace, and the Arrow keys.


Which Selenium class provides special keys?

The Keys class provides constants for special keyboard keys.


How do you send the Escape key?

Use:

element.send_keys(
    Keys.ESCAPE
)

to simulate pressing the Escape key.


Can Selenium automate Enter and Tab?

Yes. Selenium supports Enter, Tab, Escape, Arrow keys, Backspace, Delete, Home, End, Page Up, Page Down, and many other special keys through the Keys class.


Where are special keys commonly used?

They are commonly used in forms, dialogs, dropdowns, search boxes, editors, spreadsheets, enterprise applications, and accessibility testing.


Key Takeaways

  • Special keys represent non-character keyboard keys such as Enter, Escape, Tab, and Arrow keys.

  • Selenium uses the Keys class to simulate these key presses.

  • Use send_keys() together with Keys constants to automate special keys.

  • Always send special keys to the correct focused element.

  • Verify the application’s response after the key press.

  • Special key automation is an essential Selenium skill for testing modern keyboard-driven web applications.