Mace
Mace

Reputation: 11

Interacting with deeply nested radio buttons in Capybara

Recently, I've come across a radio button that seems to be so deeply nested that Capybara simply cannot interact with it.

Example of the HTML:

<div …>
   <div …>
      <div …>
         <ul …>
            <li …>
               <label …>
                 <div …>
                    <input id=”radio1” name=”Section1.Radio1” type=”radio”></input>
                    <label …> </label>  
                 </div>
               </label>
            </li>
         </ul>
      </div>
   </div>
</div>

There are about 10 more levels of depth beyond this that I've omitted before hitting the tag.

What I've Tried

I’ve tried a combination of capybara’s finders including: choose, click_on, find, find_first; all with various xpath and css combinations. These all result in a generic error “Element not found”. The xpaths and css I use are tested in Chrome and FF developer tools with no issues. The only break I’ve gotten is with the ‘all’ finder.

all(:css, #id_name).first

This seems to result in a found object, when I add ‘click’:

all(:css, #id_name).first.click

I receive an error in Capybara 'undefined method `click' for nil:NilClass (NoMethodError)'

Maybe I should go in a different direction. Anyone have any ideas?

Upvotes: 1

Views: 224

Answers (2)

cin
cin

Reputation: 63

Have you tried this?

first(:css, 'radio1').click

Upvotes: 0

Anthony
Anthony

Reputation: 15967

You can use the #choose method that Capybara provides. From the docs:

Find a radio button and mark it as checked. The radio button can be found via name, id or label text.

so in your instance:

page.choose('radio1')

Upvotes: 0

Related Questions