I\'m trying to select an item from a drop down menu using Capybara (2.1.0).
I want to select by number (meaning select the second, third, etc option).
I\'ve
none of the answers worked for me in 2017 with capybara 2.7. I got "ArgumentError: wrong number of arguments (given 2, expected 0)"
But this did:
find('#organizationSelect').all(:css, 'option').find { |o| o.value == 'option_name_here' }.select_option
another option is to add a method like this
def select_option(css_selector, value)
find(:css, css_selector).find(:option, value).select_option
end
If you take a look at the source of the select method, you can see that what it does when you pass a from
key is essentially:
find(:select, from, options).find(:option, value, options).select_option
In other words, it finds the <select>
you're interested in, then finds the <option>
within that, then calls select_option
on the <option>
node.
You've already pretty much done the first two things, I'd just rearrange them. Then you can tack the select_option
method on the end:
find('#organizationSelect').find(:xpath, 'option[2]').select_option