Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

java - 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. 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:

// 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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am using the following code to click the Escape or 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. So I am using the sleep() method. For my test case, I required to pause for 700 MillSec. Bases on your requirement you need to change the value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...