Tomo
Tomo

Reputation: 3531

How to simulate pressing enter in html text input with Selenium?

In a web interface, I've got a text field. When user enters text and accepts with enter, application performs an action.

I wanted to test the behavior with Selenium. Unfortunately, invoking 'keypress' with chr(13) insert representation of the character into the field.

Is there a way other then submitting the form? I'd like to mimic intended user interaction, without any shortcuts...

Upvotes: 9

Views: 18462

Answers (5)

user1710861
user1710861

Reputation: 423

you can use Webelement.sendkeys(Keys.Enter);

Upvotes: 0

eghm
eghm

Reputation:

I ended up using selenium.keyPress(id, "\\13");

Upvotes: 5

noah
noah

Reputation: 21519

This Java code works for me:

selenium.keyDown(id, "\\13");

Notice the escape. You probably need something like chr(\13)

Upvotes: 10

Peter Bernier
Peter Bernier

Reputation: 8069

It's been a while since I've had to do this, but I seem to recall having to use a javascript snippet to execute the carrage return as opposed to using the Selenium keypress function.

Upvotes: 0

Scott Gowell
Scott Gowell

Reputation: 485

Though I haven't tested this I imagine you can use "\r\n" appended to a string to simulate a new line. If not look for the languages equivalent to "Environment.NewLine;" ?

Upvotes: 0

Related Questions