Quality Horizon Webinar: How QA Evolves When AI Writes the Code
Live on Oct 2nd | Save your spot →
All All News Products Insights AI DevOps and CI/CD Community

How to type into a text box using Selenium?

Learn how to type into text boxes with Selenium WebDriver using sendKeys() and automate form interactions efficiently.

Hero Banner
Blog / Insights /
How to type into a text box using Selenium?

How to type into a text box using Selenium?

QA Consultant Updated on

Typing into a text box is one of the first things you'll want to do when working with Selenium. It’s a core action that helps simulate real user input to fill out things like login forms, search fields, checkout screens.

This guide will walk you through the exact steps to type into a text box using Selenium WebDriver. Whether you're new to automation or just looking for a refresher on Selenium input text techniques, this article has you covered.

We’ll break down everything you need to know, including:

  • What you need to start using Selenium
  • How Selenium identifies the right text box on the page
  • How to use sendKeys() to input text
  • Code examples for both Python and Java

Let’s get started with the basics.

How to type into a text box using Selenium

Typing into a text field is a basic but essential step in test automation. With Selenium, this is fast, reliable, and easy to maintain.

Here’s how to type into a text box using Selenium WebDriver:

  • Set up your Selenium environment with a browser driver
  • Open the target website using WebDriver
  • Locate the text box element on the page
  • Clear the input box if needed
  • Use sendKeys() to input your text
  • Optionally, validate the input value to confirm the action

Let's apply that to a real use case. Imagine you're testing the login field on https://katalon.com. Your automation script will start the browser, find the input field by ID, and use Selenium input text functionality to enter the username. It's a small step that makes automated workflows possible.

This action works the same way across login fields, email forms, or any place where user input is needed. Once you understand how selenium input text works, you can apply it to hundreds of test scenarios. It’s simple, flexible, and gets the job done.

Pre-requisites to use Selenium

Before you start typing into a text box using Selenium, you need a few things ready. This setup only takes a few minutes and gives you a strong foundation to work with.

Here’s what you need:

  • Install Java or Python: Choose one language to write your test scripts. Python is great for beginners. Java works well for enterprise setups.
  • Set up Selenium WebDriver: Download Selenium for your chosen language. Then add a browser driver like ChromeDriver to your system path.
  • Prepare your IDE: Use something you’re comfortable with. Eclipse, IntelliJ, or VS Code all work well for Selenium testing.
  • Run a sample script: This confirms your setup is working correctly and gives you confidence to move forward.

Here’s a quick Python example that opens Katalon's homepage. It uses the selenium input text setup and shows your environment is ready.

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

driver = webdriver.Chrome()
driver.get("https://katalon.com")
driver.quit()

This script loads the page and closes the browser. It’s a quick way to confirm that Selenium is working and you’re ready to type into any input field.

How Selenium identifies the text box element

Before Selenium can type into a text box, it needs to find that element on the page. This is where locators come in. A locator tells Selenium where to look and what to interact with.

Here are the most common and effective ways to identify a text input field using Selenium:

  • ID: This is often the most stable. It looks like this:
    By.id("username")
  • Name: Useful when IDs are not available:
    By.name("username")
  • Class name: Helpful when class values are unique:
    By.className("input-text")
  • XPath: Offers flexibility in finding elements based on structure:
    By.xpath("//input[@id='username']")
  • CSS selector: Another precise option for locating elements:
    By.cssSelector("input#username")

Each method helps you use the selenium input text feature in the right way. When working with dynamic applications, pick the locator that stays consistent across UI changes.

Always prefer a locator that is both unique and stable. This gives you more reliable tests and fewer false failures.

How to type into the text box using sendKeys()

The sendKeys() function is how Selenium types text into any input field. It simulates keyboard input, just like a real user would. The process is simple, but powerful.

Follow these steps to use the Selenium input text capability effectively:

  • First, locate the text box using the right locator
  • Next, clear the text box so you’re starting fresh
  • Then, type your input using sendKeys()
  • Finally, verify the text was entered as expected

Here’s how this looks in Python. The script opens the page, types into the text field, and checks the result.

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

driver = webdriver.Chrome()
driver.get("https://katalon.com")

textbox = driver.find_element(By.ID, "search")
textbox.clear()
textbox.send_keys("selenium input text")

print("Input typed:", textbox.get_attribute("value"))
driver.quit()

And here’s how you can do the same using Java. The logic stays the same, just the syntax changes.

Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class InputTextExample {
  public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
    driver.get("https://katalon.com");

    WebElement textbox = driver.findElement(By.id("search"));
    textbox.clear();
    textbox.sendKeys("selenium input text");

    System.out.println("Input typed: " + textbox.getAttribute("value"));
    driver.quit();
  }
}

Now you know exactly how to type into a field using Selenium. The sendKeys() function is fast, reliable, and works across all browsers supported by WebDriver.

Conclusion

Typing into a text box using Selenium is a simple and essential skill. Once your environment is ready, you can use the selenium input text approach in any test case. It works smoothly with forms, search fields, and custom input components.

All you need is a clear locator and the sendKeys() method. From there, your scripts become more dynamic and user-focused. Every test that uses user input starts with this step, and it scales well with any automation strategy.

As you build more advanced tests, keep refining your locators and validating your input actions. This will keep your test suite stable and easy to maintain.

Want to take this further? Explore more use cases with Katalon Studio and see how far Selenium automation can go.

Ask ChatGPT
|
Vincent N.
Vincent N.
QA Consultant
Vincent Nguyen is a QA consultant with in-depth domain knowledge in QA, software testing, and DevOps. He has 10+ years of experience in crafting content that resonate with techies at all levels. His interests span from writing, technology, building cool stuff, to music.
on this page
Click