简体   繁体   English

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

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

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.它们不会导致任何错误,但仍然没有按下回车键。

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!工作得很好!

These days (Capybara version 2.5+) you can simulate the <enter> key in the following way:现在(Capybara 2.5+ 版)您可以通过以下方式模拟<enter>键:

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

The \\n (new line) is the very important bit here. \\n (新行)是这里非常重要的一点。

Usually when you run page.execute_script, you get the same results as if you were running that in the page console.通常,当您运行 page.execute_script 时,您会得到与在页面控制台中运行相同的结果。 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.这通常是我所做的.. 在浏览器控制台窗口中制作所需的 js 代码,并在它工作时使用 execute_script 将其粘贴到水豚代码中。

Capybara doesn't have native support for a send_keys type event. Capybara 没有对 send_keys 类型事件的原生支持。 You might be able to go down to selenium to do it, or you can try this gem https://github.com/markgandolfo/send-keys您可能可以使用 selenium 来执行此操作,或者您可以尝试使用此 gem https://github.com/markgandolfo/send-keys

这个对我有用

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

@Page.selector.send_keys :return

这对我有用,其中selector是页面对象元素中的元素:selector, '<css selector>'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在输入时获得输入字段值 - How can i get input field value on hitting enter 如何区分单击提交按钮和输入输入以提交表单? - How do I differentiate between clicking a submit button and hitting enter in an input to submit a form? 如何在 React 中的“Enter”keyUp 上重置输入字段? - How do I reset an input field on 'Enter' keyUp in React? 编辑文本输入时,如何在不使用表单的onSubmit处理程序的情况下按“ Enter”键执行特定的JavaScript操作? - When editing a text input, how do I do a specific JavaScript action on hitting “Enter” key without the form's onSubmit handler? 在无格式输入字段上按Enter时,如何防止发送表单? (DNN) - How to prevent a form to be sent when hitting enter on a none-form input-field? (DNN) 我如何找到提交表单后按下输入后剩下的输入字段。 - How do I find the input field that I left from after enter pressed that submits a form. 如何使用正则表达式和JavaScript强制用户在输入字段中输入特定模式 - How do I force user to enter specific pattern in input field using regex and javascript 点击提交后如何清除输入字段 - How to clear input field after hitting submit 我怎么做 <div> 进入 <textarea> 单击“输入键盘按钮”时是否也要复制换行符? - How do I make <div> into <textarea> copy to copy the line break too when hitting “enter keyboard button”? 如何通过按回车键关注同一列中的下一行输入 - How to focus on next row input in the same column by hitting enter key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM