Jamaal
Jamaal

Reputation: 1489

How do I select an option from a select box using Capybara in Rails 4?

I'm trying to select an option from a select box using Capybara and I've tried multiple combinations and ways to select the item but Capybara says it can't find the element. Here is the HTML that it is selecting from:

   <select id="subject_id" name="subject[id]"><option value="">Select a subject</option>
    <option value="25">Biology &amp; Life Sciences</option>
    <option value="26">Business &amp; Management</option>
    <option value="27">Chemistry</option>
    <option value="28">Communication</option>
    <option value="29">Computer Science</option>
   </select>

I've tried multiple combinations such as these to select "Computer Science" which has a value of "29" from the list:

select "Computer Science", from: "subject_id"

find('subject_id').find(:xpath, 'option[29]').select_option

find('subject_id').find("option[value='29']").click

select "29", from: "subject[id]"

I know this doesn't have to do with being on the right page as I am logged in and I've run similar tests that require authentication but don't require the use of select boxes.

Those are just among the 9 or 10 different combinations I have tried so far. What is it that I am doing wrong and what can I do to stop getting this Capybara::ElementNotFound error?

Upvotes: 1

Views: 4402

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53018

To select <option value="29">Computer Science</option> option, you will need to select option as below:

select "29", from: "subject_id"

Pass the option value to select.

29 is the option value for option Computer Science.

Upvotes: 4

Related Questions