Pages

Selenium "keypress" command to press enter key with ASCII key codes

Using "keypress" command in selenium

"keypress" command in selenium is very useful when you want to press keyboard keys like “Enter” key, “Up-Down arrows” key, “Backspace” key, “Shift” key, etc.. "keypress" command works like user pressing and then releasing that key. You need to provide targeted element locator (Where you want to press and

release key) in to target column of "keypress" command and ASCII value of keyboard key in value column.

Use of “keyPressAndWait" command in selenium

“keyPressAndWait" command will press specified key on targeted element and then it will wait for completing page loading. We can use “keyPressAndWait" command where page is reloading after pressing key. See bellow examples for how to press enter key using "keypress" command.

New Test

Command

Target

Value

open

http://www.wikipedia.org/

type

xpath=//input[@type='search']

Selenium IDE

keyPress

xpath=//input[@type='search']

\13

verifyTextPresent

seleniumhq.org

In above example, “keyPress” command will press and release “Enter” key of key board. Here, “\13” is ASCII value of “Enter” Key. So in this case, selenium will works like user is typing “Selenium IDE” in search text box and then pressing “Enter” key of keyboard. You can use any key's ASCII value as per your requirement. In above example, “verifyTextPresent” will becomes fail because selenium will execute next “verifyTextPresent” command immediately after “keyPress” command so it can not find targeted text “seleniumhq.org” during page loading.

In such cases you need to use “keyPressAndWait” command so it will press “Enter” key and then wait for page to load completely. In bellow example, “verifyTextPresent” will becomes pass.