How to simulate pressing enter in html text input with Selenium?

We can simulate pressing enter in HTML text input fields using Selenium WebDriver. The sendKeys method allows us to send key presses to web elements, including the Enter key. This is commonly used for form submissions, search operations, or triggering input validation.

Syntax

Following is the syntax to simulate pressing Enter using Selenium −

element.sendKeys(Keys.ENTER);

Alternatively, you can use Keys.RETURN which produces the same result −

element.sendKeys(Keys.RETURN);

Both Keys.ENTER and Keys.RETURN represent the same key press action and can be used interchangeably in Selenium automation.

Required Import

To use the Keys class, you must import the following package −

import org.openqa.selenium.Keys;

Using Keys.ENTER

The Keys.ENTER constant simulates pressing the Enter key on the keyboard. This is the most commonly used approach for triggering form submissions or search operations.

Example

Following example demonstrates how to enter text and press Enter using Keys.ENTER

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

public class PressEnter {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\path\to\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
        
        // identify element
        WebElement searchBox = driver.findElement(By.id("gsc-i-id1"));
        searchBox.sendKeys("Selenium");
        
        // press enter with sendKeys method and pass Keys.ENTER
        searchBox.sendKeys(Keys.ENTER);
        
        driver.close();
    }
}

This code locates a search input field, enters "Selenium" as text, and then simulates pressing the Enter key to trigger the search operation.

Using Keys.RETURN

The Keys.RETURN constant is functionally identical to Keys.ENTER. Both constants represent the same key press action and produce the same results in web applications.

Example

Following example shows the same functionality using Keys.RETURN instead −

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

public class PressReturn {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\path\to\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
        
        // identify element
        WebElement searchBox = driver.findElement(By.id("gsc-i-id1"));
        searchBox.sendKeys("Selenium");
        
        // press enter with sendKeys method and pass Keys.RETURN
        searchBox.sendKeys(Keys.RETURN);
        
        driver.close();
    }
}

The behavior is identical to the previous example − the search box receives "Selenium" text followed by an Enter key press.

Alternative Approach − Using submit()

For form submissions, you can also use the submit() method on the form element instead of pressing Enter −

// Alternative approach for form submission
WebElement form = driver.findElement(By.tagName("form"));
form.submit();

However, pressing Enter is more reliable as it simulates actual user behavior and works consistently across different web applications.

Common Use Cases

The Enter key simulation is commonly used in the following scenarios −

  • Search operations − Triggering search functionality in search boxes

  • Form submissions − Submitting forms without clicking a submit button

  • Login processes − Submitting credentials after entering username and password

  • Input validation − Triggering field validation or auto-complete features

Selenium Enter Key Simulation Process 1. Locate Element findElement() 2. Enter Text sendKeys("text") 3. Press Enter sendKeys(Keys.ENTER) Key Options Keys.ENTER - Most common Keys.RETURN - Same effect

Best Practices

When simulating Enter key presses in Selenium, follow these best practices −

  • Wait for element visibility − Ensure the input element is visible and interactable before sending keys

  • Clear existing text − Use clear() method before entering new text to avoid concatenation

  • Use explicit waits − Wait for page responses or elements to load after pressing Enter

  • Handle exceptions − Implement proper error handling for element not found or interaction failures

Comparison of Methods

Method Usage Best For
Keys.ENTER element.sendKeys(Keys.ENTER) General purpose, search boxes, any input field
Keys.RETURN element.sendKeys(Keys.RETURN) Same as ENTER, alternative syntax
submit() form.submit() Form submissions only, requires form element

Conclusion

Simulating the Enter key press in Selenium is straightforward using Keys.ENTER or Keys.RETURN with the sendKeys() method. Both constants produce identical results and are commonly used for form submissions, search operations, and triggering input validation in automated testing scenarios.

Updated on: 2026-03-16T21:38:54+05:30

838 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements