Search code examples
selenium-webdrivercheckboxcapybaravisibility

Unable to find checkbox with Capybara


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?


Solution

  • Figured it out, tried this:

    find("accepts_fcra", :visible => false).click
    

    And it worked, the element was 'invisible', so just had to pass that in along with the find and click for it to work.