Javascript 如何使用 Capybara 和 ChromeDriver 模拟在输入字段中按 Enter 键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10866946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:20:26  来源:igfitidea点击:

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

javascriptjqueryruby-on-railscapybaraselenium-chromedriver

提问by Eric M.

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.

我有以下帮助方法可以将字符串输入到输入字段中并按下回车键,但似乎从未按下回车键。我看到输入字段中输入的字符串,但在按 Enter 键时发生的事件从未发生过。

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.

它们不会导致任何错误,但仍然没有按下回车键。

回答by Mysterio Man

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

为我工作。我的字段没有名称或 ID,但类型是输入的,所以我使用了类似的东西

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

works perfectly fine!

工作得很好!

回答by Pawe? Go?cicki

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

如今(Capybara 2.5+ 版)您可以<enter>通过以下方式模拟键:

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

The \nis the very important bit here.

\n是这里非常重要的一点。

回答by Felipe Lima

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.

通常,当您运行 page.execute_script 时,您会得到与在页面控制台中运行相同的结果。尝试在控制台中手动运行它,看看是否得到了预期的结果。这通常是我所做的.. 在浏览器控制台窗口中制作所需的 js 代码,并在它工作时使用 execute_script 将其粘贴到水豚代码中。

回答by DVG

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

Capybara 没有对 send_keys 类型事件的原生支持。您可能可以使用 selenium 来执行此操作,或者您可以尝试使用此 gem https://github.com/markgandolfo/send-keys

回答by Mary Dear

It works for me

这个对我有用

page.execute_script("$('form.css-class/#form_id').submit()")

回答by Murali K

@Page.selector.send_keys :return

@Page.selector.send_keys :return

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

这对我有用,selector页面对象元素中的元素在哪里:selector, '<css selector>'