在Selenium脚本中按Enter键
我使用Selenium Server(v2.21)和Selenium Java Client(v.2.21.0)来自动化每个条目后需要按下Enter键的Web表单,因为字段是根据输入的值公开的。 所以基于这里的解决方案,我一直在尝试不同的方式在表单中输入一个字符串并按Enter键 – 这是我尝试过的:
// type field value selenium.type("program", "MIC HOMEOWNERS"); // ** not working: selenium.keyPress("program", "\\13"); // ** not working: selenium.select("program", "Program"); // ** not working: selenium.keyPressNative(Keys.ENTER.toString()); // ** not working: selenium.keyDown("program", "13");
看起来这是最合乎逻辑的解决方案( selenium.keyPressNative(Keys.ENTER)
),但如果不添加.toString
,编译器会抛出错误,因为keyPressNative
需要一个String。
实际的表单代码:
Spring.addDecoration(new Spring.ElementDecoration({ elementId : "program", widgetType : "dijit.form.ValidationTextBox", widgetAttrs : { trim:true , required : true }}));
如何模拟按Enter键?
我使用以下代码单击Escape 或 Enter 。
try { Thread.sleep(700); } catch (InterruptedException e) { selenium.keyPressNative("27"); // Escape selenium.keyPressNative("10"); // Enter }
我们需要暂停selenium直到上一个命令成功执行。 所以我使用sleep()方法。 对于我的测试用例,我需要暂停700 MillSec。 根据您的要求,您需要更改值。
在WebDriver中有一个类键。 使用这个我们可以发送ENTER键。 喜欢这个
driver.findElement(By.id("elementid")).sendKeys(Keys.ENTER);
对于Selenium RC:
selenium.keyPress("elementid", "\\13"); // Note the double backslash
你试过这个吗?
selenium.keyPressNative("\n");
尝试这个。 我正在测试的应用程序要求用户执行以下步骤以将值输入表格单元格。 (单击单元格,按回车键,输入一个数字,再次按回车键完成)它适用于我的脚本
selenium.click("id_locator"); this.command_waitInSeconds(1); selenium.keyPress("id_locator", "\\13"); this.command_waitInSeconds(3); selenium.type("name=value", "10000"); selenium.focus("name=value"); selenium.keyPress("name=value", "\\13");
如果您将selenese编写为HTML(或使用IDE),则可以在变量${KEY_ENTER}
使用ENTER键。
以下是sendKeys的示例:
sendKeys | id=search | ${KEY_ENTER}
您可以在此处查看所有可用密钥的列表: http : //blog.reallysimplethoughts.com/2013/09/25/using-special-keys-in-selenium-ide-part-2/
你试过这个吗? (这是ruby)
$driver.find_element(:id, "program").send_keys [:return]
尝试使用XPATH搜索Element,然后使用以下代码。 它对我有用。
driver.findElement(By.xpath(".//*[@id='txtFilterContentUnit']")).sendKeys(Keys.ENTER);
webElem=driver.findElement(By.xpath("")); webElem.sendKeys(Keys.TAB); webElem.sendKeys(Keys.ENTER);