send_keys()

Introduction

The send_keys() method is one of the most commonly used WebElement commands in Selenium. It allows automation engineers to enter text into input fields, textareas, password fields, search boxes, and other editable web elements.

Almost every web application requires user input in some form, making send_keys() an essential command for browser automation. Whether you’re logging into an application, filling out registration forms, searching for products, or entering payment details, send_keys() plays a fundamental role in Selenium automation.

In this tutorial, you’ll learn what send_keys() is, why it is used, its syntax, practical examples, real-world use cases, common mistakes, and best practices.


What is send_keys()?

The send_keys() method is a WebElement command that simulates keyboard input by sending text or keyboard actions to a web element.

Selenium first locates the required element and then enters the specified text into it.

For example, consider the following HTML:

<input type="text">

<input type="password">

<textarea></textarea>

Selenium can enter text using:

element.send_keys("Hello World")

The send_keys() method is commonly used for interacting with:

  • Textboxes

  • Password fields

  • Search boxes

  • Textareas

  • Email fields

  • Registration forms

  • Login forms

  • Modern UI components


Why Use send_keys()?

The send_keys() method is useful because it:

  • Simulates real keyboard input.

  • Supports browser automation.

  • Works with most editable web elements.

  • Improves automation reliability.

  • Is simple and easy to use.

  • Is one of the most widely used Selenium commands.


Syntax

element.send_keys("text")

Where:

  • element → Previously located WebElement.

  • "text" → The value to be entered into the element.

  • send_keys() → Performs the keyboard input operation.


Example

The Selenium practice website contains Username and Password fields. Selenium locates both elements and enters valid credentials using the send_keys() method.

The Selenium code is:

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


# Topic: 16. Basic WebElement Commands - send_keys()
# Practice site: https://the-internet.herokuapp.com/login
# Run: pytest -s 16_examples/test_02_send_keys.py
#
# send_keys() types text into input fields, textareas, and other editable
# elements.


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

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

        username = driver.find_element(
            By.ID,
            "username"
        )

        password = driver.find_element(
            By.ID,
            "password"
        )

        username.send_keys("tomsmith")
        password.send_keys(
            "SuperSecretPassword!"
        )

        assert username.get_attribute(
            "value"
        ) == "tomsmith"

        assert password.get_attribute(
            "value"
        ) == "SuperSecretPassword!"

    finally:
        driver.quit()

Output

The Username and Password
fields are populated
successfully using the
send_keys() method.

Understanding the Code

Import the By Class

from selenium.webdriver.common.by import By

Imports Selenium’s locator strategies.

Open the Login Page

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

Launches the Selenium practice website.

Locate the Username Field

driver.find_element(
    By.ID,
    "username"
)

Locates the Username textbox.

Locate the Password Field

driver.find_element(
    By.ID,
    "password"
)

Locates the Password textbox.

Enter the Username

username.send_keys(
    "tomsmith"
)

Enters the username into the Username field.

Enter the Password

password.send_keys(
    "SuperSecretPassword!"
)

Enters the password into the Password field.

Validate the Entered Values

assert username.get_attribute(
    "value"
) == "tomsmith"

assert password.get_attribute(
    "value"
) == "SuperSecretPassword!"

Verifies that Selenium successfully entered the required values.


How send_keys() Works

            Python Script
                   │
                   ▼
            Locate Web Element
                   │
                   ▼
              send_keys()
                   │
                   ▼
          Simulate Keyboard Input
                   │
                   ▼
            Update Element Value
                   │
                   ▼
             Perform Validation

Practical Example

Suppose you’re automating a Registration page.

The application requires users to enter:

  • Full Name

  • Email Address

  • Phone Number

  • Password

Selenium uses:

element.send_keys()

to populate each field automatically during test execution.

This closely resembles real user behavior.


Automation Testing Example

Consider an online banking application.

During regression testing, Selenium uses send_keys() to automate:

  • Customer logins.

  • Fund transfer forms.

  • Payment details.

  • Search operations.

  • Registration forms.

  • Profile updates.

Professional automation frameworks extensively use send_keys() while validating user workflows across multiple environments.


Real-World Example

Automation engineers frequently use send_keys() while automating:

  • Banking applications.

  • E-Commerce websites.

  • Healthcare portals.

  • CRM systems.

  • ERP applications.

  • SaaS products.

  • Enterprise web applications.

  • Dynamic dashboards.

  • Modern JavaScript-based applications.

The send_keys() method is one of the most fundamental Selenium commands used in real-world automation projects.


Advantages of send_keys()

  • Simulates real keyboard input.

  • Simple and easy to use.

  • Works with most editable elements.

  • Improves automation reliability.

  • Produces readable automation scripts.

  • Widely supported across browsers.


Limitations of send_keys()

  • Hidden or disabled elements cannot receive keyboard input.

  • Dynamic webpages may require synchronization techniques.

  • Existing text may need to be cleared before entering new values.

  • Some custom UI components may require additional handling.


Common Mistakes Beginners Make

Forgetting to Clear Existing Values

Avoid

username.send_keys(
    "admin"
)

when the textbox already contains a value.

Prefer

username.clear()

username.send_keys(
    "admin"
)

Clearing existing values significantly improves automation reliability.


Ignoring Synchronization Issues

Sometimes an element exists on the webpage but is not yet ready to accept user input.

Always use appropriate synchronization techniques when working with dynamic webpages.


Using send_keys() on Non-Editable Elements

The send_keys() method should be used only with editable elements such as:

  • Input fields.

  • Password fields.

  • Textareas.

  • Search boxes.

  • Editable components.

Attempting to use it on non-editable elements may result in exceptions.


Best Practices

  • Verify that elements are visible and enabled before entering text.

  • Clear existing values whenever appropriate.

  • Use synchronization techniques for dynamically loaded elements.

  • Validate entered values after performing operations.

  • Keep automation scripts simple and readable.

  • Use meaningful test data during automation testing.


Conclusion

The send_keys() method is one of Selenium’s most fundamental WebElement commands. It allows automation engineers to simulate real keyboard input efficiently while keeping automation scripts clean and maintainable.

Understanding how and when to use send_keys() correctly is essential for building scalable and reliable Selenium automation frameworks used in professional software testing environments.


Frequently Asked Questions (FAQs)

What is the send_keys() method in Selenium?

The send_keys() method simulates keyboard input by entering text into editable web elements.

What is the syntax of send_keys()?

element.send_keys("text")

Which elements support send_keys()?

The send_keys() method is commonly used with:

  • Textboxes

  • Password fields

  • Search boxes

  • Textareas

  • Email fields

  • Editable UI components

Should I clear the textbox before using send_keys()?

Yes, whenever appropriate.

Clearing existing values improves automation reliability and prevents unexpected test failures.

Can send_keys() fail in Selenium?

Yes.

The send_keys() method may fail when:

  • Elements are hidden.

  • Elements are disabled.

  • Appropriate synchronization techniques are not used.

  • Non-editable elements are targeted.


Key Takeaways

  • The send_keys() method simulates real keyboard input.

  • It is one of Selenium’s most frequently used WebElement commands.

  • Use it for editable elements such as textboxes and password fields.

  • Clear existing values whenever appropriate.

  • Validate entered values after performing operations.

  • The send_keys() method is extensively used in professional Selenium automation frameworks.