Typing the Enter/Return Key in Selenium

Typing the Enter/Return Key in Selenium

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When automating web testing with Selenium, you might encounter a situation where you need to simulate pressing the Enter or Return key, especially when a form lacks a Submit button. This article provides a comprehensive guide on how to achieve this in various programming languages.

Approach for Different Programming Languages

Here are the methods to press the Enter or Return key in Selenium for different programming languages. The examples provided are based on the highest-rated and most recent solutions.

Java

In Java, you can use the Keys.RETURN or Keys.ENTER constants from the Selenium library. Below are the steps:


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

    WebDriver driver = new FirefoxDriver();
    WebElement element = driver.findElement(By.id("element_id"));
    element.sendKeys(Keys.RETURN);
    // OR
    element.sendKeys(Keys.ENTER);
    

Python

For Python, you can similarly use Keys.RETURN or Keys.ENTER from the Selenium library:


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

    driver = webdriver.Firefox()
    element = driver.find_element_by_id("element_id")
    element.send_keys(Keys.RETURN)
    # OR
    element.send_keys(Keys.ENTER)
    

Ruby

In Ruby, you can use the following approach to send the Enter key:


    element = @driver.find_element(:name, "value")
    element.send_keys "keysToSend"
    element.send_keys :return
    # OR
    @driver.action.send_keys(:enter).perform
    @driver.action.send_keys(:return).perform
    

C#

For C#, you can use the following code to simulate pressing the Enter key:


    using OpenQA.Selenium;
    using OpenQA.Selenium.Firefox;

    IWebDriver driver = new FirefoxDriver();
    IWebElement element = driver.FindElement(By.Id("element_id"));
    element.SendKeys(Keys.Return);
    // OR
    element.SendKeys(Keys.Enter);
    

Understanding the Difference Between Keys.ENTER and Keys.RETURN

Both Keys.ENTER and Keys.RETURN are used to simulate the Enter key press. Functionally, there is no difference between the two when used in Selenium. The distinction primarily exists due to historical reasons related to different operating systems and keyboards.

Conclusion

Simulating an Enter key press in Selenium is straightforward once you know the correct syntax for your programming language. Whether you use Keys.RETURN or Keys.ENTER, the result will be the same. This functionality is essential for automating forms that lack a Submit button or require keyboard interactions.

Enhancing Your Testing with Repeato

If you’re looking to streamline your mobile app testing, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android, leveraging computer vision and AI to create, run, and maintain automated tests. Its intuitive interface and fast test editing capabilities make it an excellent choice for both novice and advanced testers. Additionally, Repeato now supports testing websites inside Android emulators or devices, with explicit web testing support coming soon. Explore more about Repeato on our Getting Started page.

Like this article? there’s more where that came from!