在 Eclipse 中使用 Selenium WebDriver 和 java 代码在搜索字段中输入文本后如何按“Enter”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22729341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to press 'Enter' once text is entered in the search field using Selenium WebDriver and java code in eclipse
提问by user2691854
I am trying to write test case using WebDriver, TestNG in Eclipse. Version of WebDriver is 2.39
我正在尝试在 Eclipse 中使用 WebDriver、TestNG 编写测试用例。WebDriver 版本为 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.
在测试用例中,我试图打开浏览器,输入站点地址,加载后,找到搜索字段,使用 Excel 工作表中的数据驱动类型输入文本。
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.
输入第一个数据后,我想单击键盘上的 Return 键并等待加载并清除它并从电子表格输入下一个测试。
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,
问候,
回答by mig8
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!
我已经遇到过类似的问题。click() 甚至可以在 Selenium IDE 中运行,但不能在 jUnit 测试中运行。解决方案是使用 submit() 即使有效。(但仅在 jUnit 中;))让我们尝试一下!
回答by olyv
You can simulate hit Enter key by adding "\n" to the entered text. For example textField.sendKeys("text you type into field" + "\n")
.
您可以通过在输入的文本中添加“\n”来模拟按 Enter 键。例如textField.sendKeys("text you type into field" + "\n")
。
upd: BTW, it has been already asked here Typing Enter/Return key in Selenium
更新:顺便说一句,这里已经问过在 Selenium 中输入 Enter/Return 键
回答by xyz
You can use
您可以使用
driver.findElement(By.id("IDValue")).sendKeys(Keys.ENTER);
回答by Ivan Infante
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);
.
这是我用作在字段中输入多个值的示例: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);
.
回答by Ruby Tester
Using this snippet you can skip using Enter key
使用此代码段,您可以使用 Enter 键跳过
driver.get("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("your text");
element.submit();