Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

rspec - 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.

Here is the checkbox

<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)

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.

Update

I have also tried selecting the element through xpath. Here are the various things I have found out

find(:xpath, '//*[@id="registration"]/span[2]')

This is able to find the span element no problem

find(:xpath, '//*[@id="registration"]/span[2]/input')

This can't find the element I need, but this xpath correctly selects the element in chrome's console

find(:xpath, '//*[@id="agreement"]')

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')

This is able to find the label element in the span with no problem.

question from:https://stackoverflow.com/questions/21033560/capybara-unable-to-find-css

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

You can also add the following to env.rb to enable Capybara to interact with all hidden elements:

Capybara.ignore_hidden_elements = false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...