7

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,

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
user2691854
  • 71
  • 1
  • 1
  • 2
  • possible duplicate of [Typing Enter/Return key in Selenium](http://stackoverflow.com/questions/1629053/typing-enter-return-key-in-selenium) – Louis Mar 29 '14 at 11:11

5 Answers5

9

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

Community
  • 1
  • 1
olyv
  • 3,699
  • 5
  • 37
  • 67
  • Sending \n appears not to be foolproof , while Keys.Enter does seem to do the job in such cases. – jeremy_rutman May 03 '21 at 14:53
  • It depends on webdriver version, ja framework, implementation of particular text element. So, this is one of many possible options – olyv Nov 03 '22 at 09:26
4

You can use

driver.findElement(By.id("IDValue")).sendKeys(Keys.ENTER);
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
4

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();
Ruby Tester
  • 113
  • 10
3

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);.

-1

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!

mig8
  • 122
  • 4
  • driver.findElement(By.cssSelector("li.search > #Form_getSearchForm > fieldset > #Terms > div.middleColumn > #Form_getSearchForm_Terms")).sendKeys(s.getCell(0,i).getContents() "\n"); got syntax error on token "\n" – user2691854 Mar 29 '14 at 14:10
  • Hm, By.cssSelector() is not the best, I think. It uses a little bit complex target... – mig8 Mar 30 '14 at 17:58