- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to specify "ENTER" button functionality in Selenium WebDriver code?
To specify ENTER button functionality in Selenium webdriver we have to use the method sendKeys. To simulate pressing the ENTER button,we have to add the statement import org.openqa.selenium.Keys to our code.
Then pass the parameter – Keys.RETURN or Keys.ENTER to the sendKeys method.
Let us make an attempt to press the ENTER button after entering some text in the Google search input box −
Example
Code Implementation with Keys.ENTER
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Keys; public class EnterOperation{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.manage().window().maximize(); //URL launch driver.get("https://www.google.com/"); // identify element WebElement e =driver.findElement(By.name("q")); e.sendKeys("Java"); // Keys.ENTER with sendKeys e.sendKeys(Keys.ENTER); } }
Code Implementation with Keys.RETURN
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Keys; public class EnterOperationReturn{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.manage().window().maximize(); //URL launch driver.get("https://www.google.com/"); // identify element WebElement r =driver.findElement(By.name("q")); r.sendKeys("Java"); // Keys.RETURN with sendKeys r.sendKeys(Keys.RETURN); } }
Output
- Related Articles
- How to automate drag & drop functionality using Selenium WebDriver Java?
- How to get HTTP Response Code using Selenium WebDriver?
- How to get Response Status Code with Selenium WebDriver?
- How to simulate Print screen button using selenium webdriver in Java?
- How to submit a form in Selenium webdriver if submit button can't be identified?
- How to Download & Install Selenium WebDriver?
- How to record a video in Selenium webdriver?
- How to get Tooltip Text in Selenium Webdriver?
- How to do session handling in Selenium Webdriver?
- How to trigger the HTML button after hitting enter button in the textbox using JavaScript?
- How to press ENTER inside an edit box in Selenium?
- How to handle frames in Selenium Webdriver in Python?
- How to take screenshot with Selenium WebDriver?
- How to hide Firefox window (Selenium WebDriver)?
- How to send cookies with selenium webdriver?

Advertisements