user2691854
user2691854

Reputation: 71

How to press 'Enter' once text is entered in the search field using Selenium WebDriver and java code in eclipse

I am trying to write test case using WebDriver, TestNG in Eclipse. Version of WebDriver is 2.39

In the test case I am trying to open a browser, enter site address, once it is loaded, find search field, enter text using Datadriven type from an excel sheet.

Once the first data is entered, I would like to click Return key on keyboard and wait till loads and clear it and enter next test from spreadsheet.

I am successfull in entering text,clearing, but not sure how to write code to press 'Return key' or Enter, please advise.

Apologies, I could not find this in search.

regards,

Upvotes: 7

Views: 56961

Answers (5)

Ruby Tester
Ruby Tester

Reputation: 113

Using this snippet you can skip using Enter key

driver.get("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("your text");
element.submit();

Upvotes: 4

Ivan Infante
Ivan Infante

Reputation: 67

This is what I use as an example to enter multiple values into a field:
driver.findElement(By.xpath("(//INPUT[@class='attr-new ng-pristine ng-untouched ng-valid ng-scope placeholder'])[2]")).sendKeys("Linoleum" + Keys.ENTER + "Metal" + Keys.ENTER + "Electrical" + Keys.ENTER + "Lumber" + Keys.ENTER + "Fiberglass" + Keys.ENTER + "Masonry" + Keys.ENTER + "Paint" + Keys.ENTER + "Millwork" + Keys.ENTER + "Wood" + Keys.ENTER + "Pick Ups" + Keys.ENTER);.

Upvotes: 3

Ajinkya
Ajinkya

Reputation: 22710

You can use

driver.findElement(By.id("IDValue")).sendKeys(Keys.ENTER);

Upvotes: 5

olyv
olyv

Reputation: 3817

You can simulate hit Enter key by adding "\n" to the entered text. For example textField.sendKeys("text you type into field" + "\n").

upd: BTW, it has been already asked here Typing Enter/Return key in Selenium

Upvotes: 11

mig8
mig8

Reputation: 122

I have already met a similar problem. The click() even is working from Selenium IDE but not from jUnit test. The solution was the using of submit() even that works. (But only in jUnit ;) ) Let's try it!

Upvotes: -1

Related Questions