Text Boxes

Introduction

A Text Box is one of the most commonly used form elements in web applications. It allows users to enter information such as usernames, passwords, email addresses, phone numbers, and search keywords.

In Selenium, text boxes are automated using methods like send_keys() to enter data and clear() to remove existing text before entering new values.

In this tutorial, you’ll learn how to locate and interact with text boxes using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.


What is a Text Box?

A Text Box is an HTML input field where users can type text.

Common examples include:

  • Username

  • Password

  • Email Address

  • Search Box

  • Phone Number

  • First Name

  • Last Name

Example HTML:

<input type="text" id="username" name="username">

Why Automate Text Boxes?

Automating text boxes helps you:

  • Enter user credentials.

  • Fill registration forms.

  • Submit search queries.

  • Test input validation.

  • Verify form functionality.


Common Methods Used

MethodPurpose
send_keys()Enter text into the textbox
clear()Remove existing text
get_attribute(“value”)Retrieve the entered value
is_displayed()Verify the textbox is visible
is_enabled()Verify the textbox is enabled

Example

The following example enters values into the Username and Password text boxes on The Internet Herokuapp login page and verifies that the entered values are correct.

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


# Topic: 25. Form Elements - Text Boxes
# Practice site: https://the-internet.herokuapp.com/login
# Run: pytest -s 25_examples/test_01_text_boxes.py
#
# Text boxes are input elements located by ID or name and filled with
# send_keys().


def test_text_boxes():
    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 text boxes are filled successfully with the provided values.

  • Username : tomsmith

  • Password : SuperSecretPassword!


Understanding the Code

Import the Required Classes

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

Imports:

  • webdriver for launching and controlling the browser.

  • By for locating web elements using Selenium’s locator strategies.

Create the WebDriver

driver = webdriver.Chrome()

Launches a new Chrome browser session.

Open the Practice Website

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

Opens The Internet Herokuapp login page that contains the Username and Password text boxes.

Locate the Text Boxes

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

Locates both text boxes using their unique id attributes.

Using By.ID is one of the fastest and most reliable locator strategies when unique IDs are available.

Enter Text into the Text Boxes

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

The send_keys() method simulates keyboard input and enters the specified values into the text boxes.

After execution:

  • The Username field contains tomsmith.

  • The Password field contains SuperSecretPassword!.

Verify the Entered Values

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

The get_attribute("value") method retrieves the text currently present inside the text box and verifies that the expected values were entered successfully.

If both assertions pass successfully, it confirms that Selenium entered the correct values into both text boxes.

Close the Browser

driver.quit()

Closes the browser and ends the WebDriver session.

This is a recommended practice to ensure that all browser instances are properly terminated after test execution.


Clearing Existing Text

Sometimes a textbox already contains data.

Use the clear() method before entering new text.

Example:

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

username.clear()

username.send_keys("tomsmith")

Reading the Entered Value

You can verify the entered text using the value attribute.

Example:

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

username.send_keys("tomsmith")

print(username.get_attribute("value"))

Output

tomsmith

Practical Example

Suppose an online shopping website has a search box.

The automation script enters:

Laptop

and clicks the Search button to verify that relevant products are displayed.


Automation Testing Example

Consider an online banking application.

The login page contains:

  • Username textbox

  • Password textbox

The automation script:

  • Enters the username.

  • Enters the password.

  • Clicks the Login button.

  • Verifies that the dashboard loads successfully.


Real-World Example

Text boxes are commonly used in:

  • Banking applications

  • E-commerce websites

  • CRM systems

  • Healthcare portals

  • HR management systems

  • Government websites

  • Enterprise web applications

Almost every web application contains one or more text boxes.


Advantages of Automating Text Boxes

  • Simulates real user input.

  • Validates form functionality.

  • Supports data-driven testing.

  • Improves automation coverage.

  • Reduces manual effort.


Common Mistakes Beginners Make

Forgetting to Clear Existing Text

If a textbox already contains text, using send_keys() appends the new text.

Instead, use:

textbox.clear()

before entering new data.

Trying to Enter Text into a Disabled Textbox

Always verify that the textbox is enabled.

Example:

textbox.is_enabled()

Using Incorrect Locators

Always use stable locators such as:

  • ID

  • Name

  • CSS Selector

Avoid fragile XPath expressions whenever possible.

Ignoring Synchronization

If the textbox loads dynamically, use Explicit Wait before interacting with it.


Best Practices

  • Prefer locating text boxes using ID or Name.

  • Use clear() before entering new text when necessary.

  • Verify the textbox is visible and enabled.

  • Use Explicit Wait for dynamically loaded forms.

  • Validate entered values using get_attribute("value").


Conclusion

Text boxes are among the most frequently automated web elements in Selenium. By using methods such as send_keys(), clear(), and get_attribute("value"), you can reliably interact with input fields across different web applications. Following best practices such as using stable locators and proper synchronization helps create robust and maintainable automation scripts.


Frequently Asked Questions (FAQs)

Which method is used to enter text into a textbox?

Use:

send_keys()

How do I remove existing text from a textbox?

Use:

clear()

How can I verify the entered text?

Use:

get_attribute("value")

Which locator is best for locating a textbox?

The preferred locators are:

  • ID

  • Name

  • CSS Selector

Are text boxes commonly automated in Selenium?

Yes.

Text boxes are one of the most frequently automated web elements because they are used in login forms, registration forms, search bars, and many other web application features.


Key Takeaways

  • Text boxes allow users to enter information into web forms.

  • Use send_keys() to enter text.

  • Use clear() to remove existing text.

  • Use get_attribute("value") to verify entered text.

  • Prefer stable locators such as ID and Name.

  • Use Explicit Wait when text boxes load dynamically.

  • Proper synchronization and locator selection improve automation reliability.