How do I simulate hitting enter in an input field with Capybara and ChromeDriver?

Eric M. picture Eric M. · Jun 3, 2012 · Viewed 31.8k times · Source

I have the following helper method to input a string into an input field and press the enter key, but it seems the enter key is never pressed. I see the string entered into the input field, but the events that take place upon hitting enter never happened.

I've tested in an actual browser that the enter key correctly fires the expected events. I'm not sure what I'm missing.

def fill_and_trigger_enter_keypress(selector, value)
  page.execute_script %Q(
                          var input = $('#{selector}');
                          input.val('#{value}');
                          input.trigger("keypress", [13]);
                         )
end

EDIT:

I've also tried the following to no avail:

find('#q_name').native.send_keys(:return)
find('#q_name').native.send_keys(:enter)

They don't cause any error, but still no enter key pressed.

Answer

Mysterio Man picture Mysterio Man · Jul 26, 2012
find('#q_name').native.send_keys(:return)

works for me. I dont have a name or id for my field but the type is input so i used something like

find('.myselector_name>input').native.send_keys(:return)

works perfectly fine!