简体   繁体   English

在 Eclipse 中使用 Selenium WebDriver 和 java 代码在搜索字段中输入文本后如何按“Enter”

[英]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.我正在尝试在 Eclipse 中使用 WebDriver、TestNG 编写测试用例。 Version of WebDriver is 2.39 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,问候,

You can simulate hit Enter key by adding "\\n" to the entered text.您可以通过在输入的文本中添加“\\n”来模拟按 Enter 键。 For example textField.sendKeys("text you type into field" + "\\n") .例如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 键

您可以使用

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

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

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. click() 甚至可以在 Selenium IDE 中运行,但不能在 jUnit 测试中运行。 The solution was the using of submit() even that works.解决方案是使用 submit() 即使有效。 (But only in jUnit ;) ) Let's try it! (但仅在 jUnit 中;))让我们尝试一下!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java在Selenium WebDriver中按“TAB”然后按“ENTER”键? - How to press “TAB” then “ENTER” key in Selenium WebDriver using Java? 如何使用Java和Selenium2 / Webdriver将明天的日期输入文本字段 - How can I enter tomorrow's date into a text field using Java and Selenium2/Webdriver 如何使用 Java 在 Selenium WebDriver 的隐藏字段中输入一些文本 - How to type some text in hidden field in Selenium WebDriver using Java 如何在 selenium webdriver 的文本字段中一个一个地输入字符? - How to enter characters one by one in to a text field in selenium webdriver? 如何通过与Java一起使用Selenium WebDriver从此HTML代码中获取文本 - How to get Text from this HTML code By using Selenium WebDriver with Java 在Selenium Webdriver中,在文本框中输入文本后单击按钮 - In Selenium Webdriver, click button once the text is entered in text box Java Selenium的代码片段中没有“值”属性或文本时,如何使电子邮件/文本输入字段 - How to get the Email/Text entered into a field when there is no “value” attribute or text in the code snippet for Java Selenium 如何使用Java在Selenium WebDriver中按“ALT + S” - How to press “ALT+S” in Selenium WebDriver using Java 当元素属性不存在时,使用java在Selenium WebDriver中按ENTER键 - Press ENTER key in Selenium WebDriver with java when element property not present 如何使用Selenium Webdriver在联系人表单中输入文本? - How to Enter Text in Contact form using selenium Webdriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM