如何使用Capybara和ChromeDriver模拟输入字段中的输入? [英] How do I simulate hitting enter in an input field with Capybara and ChromeDriver?

查看:302
本文介绍了如何使用Capybara和ChromeDriver模拟输入字段中的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

编辑:

我也试过以下内容无济于事:

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)

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

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)

完美无缺!

这篇关于如何使用Capybara和ChromeDriver模拟输入字段中的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆