水豚:找不到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
元素没问题
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')
这可以找到 label
元素 span
没问题。
This is able to find the label
element in the span
with no problem.
推荐答案
昨天我遇到了确切的问题。水豚因为不可见而自动忽略了输入。我用以下方法解决了这个问题:
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屋!