Capybara: select('United States', from: 'model_country') ambiguous

Created on 14 Nov 2012  路  13Comments  路  Source: teamcapybara/capybara

select('United States', from: 'model_country') produces a Capybara::Ambiguous error.

I know find(:select, 'model_country').first(:option, 'United States').select_option works, but that seems pretty long winded.

This is because most country select dropdowns have "United States" and "United States Minor Outlying Islands". At least, from the carmen gem.

Most helpful comment

@radanskoric in Capybara 2.1 (which will be released next tuesday), you'll be able to do:

select "United States", from: "Country", :match => :first

All 13 comments

Hmm, that's strange, are you sure that "United States" is not appearing twice? Option matches need to be exact IIRC, see https://github.com/jnicklas/xpath/blob/master/lib/xpath/html.rb#L142

You're right. United States is appearing twice. I guess I have to use the longer expression above.

Just hit this issue as well, is there a way it could compare the option elements it finds and if they are all exactly the same then it just uses the first one?

Readability really suffers in this instance (although I can see the end goal for not allowing ambiguous referencing). I had to change

select "Australia", from: "country"

to

find(:select, "country").first(:option, 'Australia').select_option

Got exactly the same issue here. It should be pretty common since often, in country selects, United States and maybe Canada are duplicated on top of the select since most of the users will be selecting those.

Obviously both appearances have the same value ( 'US' ).

@radanskoric in Capybara 2.1 (which will be released next tuesday), you'll be able to do:

select "United States", from: "Country", :match => :first

Thanks, great work!

@jnicklas Awesome, thanks for providing this. I just ran into this same problem as well.

So, just an FYI, if you have a timezone_select with "Samoa" and "American Samoa", both will match as far as I can tell. In order to work around this, I had to drop into custom selector goo (I couldn't seem to figure out a way to get the #select method to work):

# Actually using ActiveSupport::TimeZone.sample.name in my code
new_tz = ['Samoa', 'American Samoa'].sample  
# ...
# Had to replace #select(new_tz, from: 'Time zone', match: :prefer_exact) with:
find(:select, 'Time zone').find("option[value='#{new_tz}']", exact: true).select_option

@dcoder2099 Have you tried select(new_tz, from: 'Time zone', exact: true)?

Yes, and that doesn't work because the #find under the covers attempts to find an

@dcoder2099 that's an entirely different issue. Capybara doesn't allow you to pick options by value, instead you must specify the text. This is intentional behaviour.

Derp. As soon as you said that, @jnicklas, I realize that makes perfect sense. Just a bit harder test to write.

:+1:

Was this page helpful?
0 / 5 - 0 ratings