用水豚点击输入

Clicking on an input with capybara

我想知道如何在水豚中点击输入。

到目前为止我已经试过了

click_on('#js-emu-submit.button.pl3.pr3.mb0.mr1')
click('js-emu-submit')
find('input', exact_text: 'Get an Estimate', match: :first).click

None 人成功了

这是来自网页的HTML。

<input type="submit" name="commit" value="Get an Estimate" id="js-emu-submit" class="button pl3 pr3 mb0 mr1" data-disable-with="Get an Estimate">

我只是想知道如何点击该项目。

查看 click_on - https://www.rubydoc.info/gems/capybara/Capybara/Node/Actions#click_link_or_button-instance_method - you can see that it's a combination of click_button and click_link and says to check each of those for the type of locator it accepts. Looking at click_button, https://www.rubydoc.info/gems/capybara/Capybara/Node/Actions#click_button-instance_method 的文档,您可以看到它会找到 any <input> element of type submit, reset, image, button(您的 HTML 元素是)并且定位器可以是 id, Capybara.test_id attribute, value, or title 中的任何一个。 CSS 选择器不是其中之一,因此这解释了为什么您的 click_on 尝试失败了。假设您的 元素在页面上可见,那么以下任何一项都应单击它

click_on 'js-emu-submit' # match on id
click_button 'js-emu-submit' # id
click_button 'Get an Estimate' # match on value - should work with `click_on` too

另一种选择是仅对位于其他位置的元素使用 clickclick 不使用定位器 - https://www.rubydoc.info/gems/capybara/Capybara/Node/Element#click-instance_method - 所以这解释了为什么你的第二次尝试不起作用,并且输入元素在元素内没有子文本所以这解释了为什么你的第三次尝试没有工作。使用 click 应该工作的东西是

find('#js-emu-submit').click # find element by CSS id selector and click it
find(:button, 'js-emu-submit').click # find by id using the :button selector
find('input[value="Get an Estimate"]').click # find by CSS attribute selector
...