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

前端 未结 6 1312
我在风中等你
我在风中等你 2020-12-24 11:42

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

相关标签:
6条回答
  • 2020-12-24 12:00

    Usually when you run page.execute_script, you get the same results as if you were running that in the page console. Try running that manually in the console and see if you get the expected results. That is usually what I do.. craft the needed js code in the browser console window and paste it into the capybara code when it is working, using execute_script.

    0 讨论(0)
  • 2020-12-24 12:04

    It works for me

    page.execute_script("$('form.css-class/#form_id').submit()")
    
    0 讨论(0)
  • 2020-12-24 12:07

    Capybara doesn't have native support for a send_keys type event. You might be able to go down to selenium to do it, or you can try this gem https://github.com/markgandolfo/send-keys

    0 讨论(0)
  • 2020-12-24 12:09
    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!

    0 讨论(0)
  • 2020-12-24 12:12

    These days (Capybara version 2.5+) you can simulate <enter> key in the following way:

    find('.selector').set("text\n")
    

    The \n is the very important bit here.

    0 讨论(0)
  • 2020-12-24 12:25

    @Page.selector.send_keys :return

    This works for me, where selector is the element in your page object element :selector, '<css selector>'

    0 讨论(0)
提交回复
热议问题