水豚:无法找到 css [英] Capybara: Unable to find css
问题描述
我正在使用水豚点击复选框,但无论我做什么似乎都找不到它.我能够正确地找到跨度内的跨度和标签,但无法找到我实际需要的输入.
I am using capybara to click a checkbox, however it can't seem to find it no matter what I do. I am able to correctly find both the span and the label inside the span, but not the input I actually need.
这是复选框
<span class="checkbox tos">
<input id="agreement" name="agreement" onclick="agreeValidate();" type="checkbox" value="false">
<label for="agreement">I accept the <a href="http://www.dev.com:3000/terms" target="_blank">Terms of Use</a>, <a href="http://www.dev.com:3000/privacy" target="_blank">Privacy Policy</a>, and am at least 13 years old</label>
</span>
这是我尝试过的一些事情
And here are some of the things I have tried
page.check('agreement')
find(:css, '#agreement').set(true)
find('#agreement').set(true)
find('#agreement').click
然而,他们都给我同样的错误
However, they all give me the same error
Unable to find css "#agreement" (Capybara::ElementNotFound)
我还想知道,当单击复选框时,这些方法中的任何一个是否会触发 onclick
方法?我觉得 find(:css, '#agreement').set(true)
不会触发 onclick
事件.但是,我不确定其余的.
I am also wondering will any of these methods fire off the onclick
method, when the checkbox is clicked? I feel like find(:css, '#agreement').set(true)
will not trigger the onclick
event. However, I am not sure about the rest.
更新
我也尝试过通过 xpath 选择元素.这是我发现的各种事情
I have also tried selecting the element through xpath. Here are the various things I have found out
find(:xpath, '//*[@id="registration"]/span[2]')
这样可以找到span
元素没问题
This is able to find the span
element no problem
find(:xpath, '//*[@id="registration"]/span[2]/input')
这找不到我需要的元素,但是这个 xpath 正确地选择了 chrome 控制台中的元素
This can't find the element I need, but this xpath correctly selects the element in chrome's console
find(:xpath, '//*[@id="agreement"]')
这找不到我需要的元素,但是 xpath 选择了 chrome 控制台中的元素
This can't find the element I need, but the xpath selects the element in chrome's console
find(:xpath, '//*[@id="registration"]/span[2]/label')
这样就可以毫无问题地找到 span
中的 label
元素.
This is able to find the label
element in the span
with no problem.
推荐答案
我昨天遇到了确切的问题.Capybara 会自动忽略输入,因为它是不可见的.我用以下方法解决了它:
I had the exact issue yesterday. Capybara was automatically ignoring the input due to it being invisible. I solved it with the following:
find('#agreement', :visible => false).click
您还可以将以下内容添加到 env.rb 以使 Capybara 与所有隐藏元素交互:
You can also add the following to env.rb to enable Capybara to interact with all hidden elements:
Capybara.ignore_hidden_elements = false
这篇关于水豚:无法找到 css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!