Unable to find checkbox with Capybara

Viewed 5222

Unable to find checkbox with Capybara.

This is the HTML:

<div class='check-group'>
 <div class='checkbox'>
   <input type="checkbox" name="accepts_fcra" id="accepts_fcra" value="1" />
   <label for="accepts_fcra"><span>Some text <a title="FCRA" target="_blank" href="https://www.google.com/fcra">FCRA</a> some text.</span></label>
 </div>
</div>

Now I want to check the checkbox with the id "accepts_fcra".

I've tried a bunch of stuff and all pretty much return the same thing: "Unable to find (method used to find checkbox)"

Some attempts:

check("#accepts_fcra")
Capybara::ElementNotFound: Unable to find checkbox "#accepts_fcra"

find("#accepts_fcra").set true
Capybara::ElementNotFound: Unable to find css "#accepts_fcra"

within("div.checkbox") do
  find(:xpath, "//input[@id='accepts_fcra']")
end
Capybara::ElementNotFound: Unable to find xpath "//input[@id='accepts_fcra']"

But the 2 classes above the checkbox, ".check-group" and ".checkbox" are found, just the checkbox is not found. Thoughts?

2 Answers

The visible: false solution didn't work for me.

This was because the checkbox was hidden, like so

 input[type="checkbox"]{
    position: absolute;
    opacity: 0;
    cursor: pointer;
    height: 0;
    width: 0;
    opacity:0;
}

and a funky checkmark style was being used instead.

However, clicking the label worked for me

eg./

page.find(:xpath, "//label[@for='accepts_fcra']").click

Related