简体   繁体   English

在Selenium脚本中按Enter键

[英]Press Enter key in Selenium script

I am using Selenium Server (v2.21) and Selenium Java Client (v.2.21.0) to automate a web form that needs to have the Enter key pressed after each entry, since fields are exposed based on the value entered. 我正在使用Selenium Server(v2.21)和Selenium Java Client(v.2.21.0)来自动化每个条目后需要按Enter键的Web表单,因为字段是根据输入的值公开的。 So based on the solution here , I've been trying different ways to enter a string into the form and press Enter - here are the ones that I've tried: 所以基于这里的解决方案,我一直在尝试不同的方式在表单中输入字符串并按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");

It would seem like this is the most logical solution ( selenium.keyPressNative(Keys.ENTER) ), but the compiler throws an error if you don't add the .toString , since keyPressNative is expecting a String. 看起来这是最合乎逻辑的解决方案( selenium.keyPressNative(Keys.ENTER) ),但是如果不添加.toString ,编译器会抛出错误,因为keyPressNative需要一个String。

The actual form code: 实际的表单代码:

<label  >Program</label> 
    <input id="program" name="program1" class="readonly-bg" readonly="readonly" type="text" value="MIC HOMEOWNERS" size="12"/>
    <input id="program" name="program" type="hidden" value="601"/>
        <script type="text/javascript">  
            Spring.addDecoration(new Spring.ElementDecoration({  
                elementId : "program",  
                widgetType : "dijit.form.ValidationTextBox",  
                widgetAttrs : { 
                    trim:true ,
                    required : true
                }}));  
        </script>
<br>

How do I emulate the press of the Enter key? 如何模拟按Enter键?

I am using the following code to click the Escape or Enter . 我使用以下代码单击Escape Enter

try {
    Thread.sleep(700);
} catch (InterruptedException e) {
    selenium.keyPressNative("27"); // Escape
    selenium.keyPressNative("10"); // Enter 
}   

we need to pause the selenium till previous command get executed successfully. 我们需要暂停selenium直到上一个命令成功执行。 So I am using the sleep() method. 所以我使用sleep()方法。 For my test case, I required to pause for 700 MillSec. 对于我的测试用例,我需要暂停700 MillSec。 Bases on your requirement you need to change the value. 根据您的要求,您需要更改值。

In WebDriver there is a class keys. 在WebDriver中有一个类键。 Using this we can send the ENTER key. 使用这个我们可以发送ENTER键。 Like this 像这样

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

For Selenium RC: 对于Selenium RC:

selenium.keyPress("elementid", "\\13"); // Note the double backslash

你试过这个吗?

selenium.keyPressNative("\n");

Try this. 尝试这个。 The application , which I am testing, requires a user to do the following steps for entering a value into a table cell. 我正在测试的应用程序要求用户执行以下步骤以将值输入表格单元格。 (click the cell, press enter key, enter a number, press enter again to complete) It is working for my script (单击单元格,按回车键,输入一个数字,再次按回车键完成)它适用于我的脚本

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

If you are writing your selenese as HTML (or with the IDE), the ENTER key is available in a the variable ${KEY_ENTER} 如果您将selenese编写为HTML(或使用IDE),则可以在变量${KEY_ENTER}使用ENTER键。

Here is a example with sendKeys: 以下是sendKeys的示例:

sendKeys | id=search | ${KEY_ENTER}

You can see a list of all available keys here: http://blog.reallysimplethoughts.com/2013/09/25/using-special-keys-in-selenium-ide-part-2/ 您可以在此处查看所有可用密钥的列表: http//blog.reallysimplethoughts.com/2013/09/25/using-special-keys-in-selenium-ide-part-2/

Have you tried this ? 你试过这个吗? (this is in ruby) (这是红宝石)

$driver.find_element(:id, "program").send_keys [:return]

Try to Use XPATH for searching the Element and then use the Following code. 尝试使用XPATH搜索Element,然后使用以下代码。 It worked for me. 它对我有用。

 driver.findElement(By.xpath(".//*[@id='txtFilterContentUnit']")).sendKeys(Keys.ENTER);
webElem=driver.findElement(By.xpath(""));
webElem.sendKeys(Keys.TAB);
webElem.sendKeys(Keys.ENTER);

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

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