How to select option in drop down using Capybara

RubySelenium WebdriverCapybara

Ruby Problem Overview


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 Googled like crazy trying all sorts of things but no luck.

I was able to select it by using the value:

 find("option[value='4c430d62-f1ba-474f-8e8a-4452c55ea0a8']").click

But I don't want to use that method b/c the value is something that will change and that will make my test brittle.

The HTML for the drop down is:

<td class="value">
    <select name="organizationSelect" id="organizationSelect" class="required">
     <option value="NULL">Choose...</option>
     <option value="4c430d62-f1ba-474f-8e8a-4452c55ea0a8">&nbsp;Institution1</option>
     <option value="e1a4efa7-352d-410a-957e-35c8a3b92944">&nbsp;Institution / test</option>
    </select>
</td>

I also tried this:

  option = find(:xpath, "//*[@id='organizationSelect']/option[2]").text  
  select(option, :from => organizationSelect)

But it results in this error:

Ambiguous match, found 2 elements matching option "Institution" (Capybara::Ambiguous)

So how can I select the first, second, third, etc option from the drop down (using Capybara) ?

Ruby Solutions


Solution 1 - Ruby

For some reason it didn't work for me. So I had to use something else.

select "option_name_here", :from => "organizationSelect"

worked for me.

Solution 2 - Ruby

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

Solution 3 - Ruby

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

Solution 4 - Ruby

To add yet another answer to the pile (because apparently there's so many ways of doing it depending on your setup) - I did it by selecting the literal option element and clicking it

find(".some-selector-for-dropdown option[value='1234']").select_option

It's not very pretty, but it works :/

Solution 5 - Ruby

Unfortunately, the most popular answer did not work for me entirely. I had to add .select_option to end of the statement

select("option_name_here", from: "organizationSelect").select_option

without the select_option, no select was being performed

Solution 6 - Ruby

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

Solution 7 - Ruby

Here's the most concise way I've found (using capybara 3.3.0 and chromium driver):

all('#id-of-select option')[1].select_option

will select the 2nd option. Increment the index as needed.

Solution 8 - Ruby

In Capybara you can use only find with xpath

find(:xpath, "//*[@id='organizationSelect']/option[2]").click

and method click

Solution 9 - Ruby

It is not a direct answer, but you can (if your server permit):

  1. Create a model for your Organization; extra: It will be easier to populate your HTML.

  2. Create a factory (FactoryGirl) for your model;

  3. Create a list (create_list) with the factory;

  4. 'pick' (sample) a Organization from the list with:

    Random select

    option = Organization.all.sample

    Select the FIRST(0) by id

    option = Organization.all[0]

    Select the SECOND(1) after some restriction

    option = Organization.where(some_attr: some_value)[2] option = Organization.where("some_attr OP some_value")[2] #OP is "=", "<", ">", so on...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFarooqView Question on Stackoverflow
Solution 1 - RubyRVMView Answer on Stackoverflow
Solution 2 - Rubycarols10centsView Answer on Stackoverflow
Solution 3 - RubymontrealmikeView Answer on Stackoverflow
Solution 4 - Rubyuser2490003View Answer on Stackoverflow
Solution 5 - RubySam DView Answer on Stackoverflow
Solution 6 - RubybjelliView Answer on Stackoverflow
Solution 7 - RubypdueyView Answer on Stackoverflow
Solution 8 - RubyAlexShView Answer on Stackoverflow
Solution 9 - RubyDavid V. TeixeiraView Answer on Stackoverflow