简体   繁体   中英

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,

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

您可以使用

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

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

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM