capybara-select2 icon indicating copy to clipboard operation
capybara-select2 copied to clipboard

`Ambiguous match` when there are multiple select2 elements

Open dgmstuart opened this issue 9 years ago • 7 comments

#37 changed the code which finds the select2 input from using first to using find. Unfortunately Capybara chokes when it can't find a unique element with find - which happens when you have more than one select2 element in the page:

Failure/Error: fill_event_fields_with_valid_data Capybara::Ambiguous: Ambiguous match, found 2 elements matching css ".select2-container"

I don't really understand enough about the issue that #37 fixes to know what the solution should be.

(cc @machisuji)

dgmstuart avatar Nov 09 '15 01:11 dgmstuart

The code in "capybara-select2/gem/lib/capybara-select2.rb" has a section with "if options.has_key? :search" that uses only a find command to select the element, no use of first to resolve the ambiguous matches. Note that if i have "name" and "city name" i am in the case of an ambiguous match.

thomasmetal avatar Dec 28 '15 14:12 thomasmetal

This can be fixed by having find(..., match: :first)

fluke avatar May 10 '16 07:05 fluke

So would it be fixed by using a more specific selector when you have two select2s? I.e.

select2 'value', css: '#selectid'

I mean just picking the first one in an ambiguous match is not the right solution IMO. It's ambiguous for a reason.

machisuji avatar May 10 '16 08:05 machisuji

@machisuji What you're saying is probably correct. The older first is just better represented as find(.., match: :first) if we want to keep the old behavior.

fluke avatar May 10 '16 08:05 fluke

Hmm. I actually agree with @machisuji - it is an ambiguous match, so the current behaviour is correct.

I guess it's arguable whether this change is breaking or a bugfix ;)

I think if there's a 'fix' to be made at all then it would be to add a message into the error suggesting using the :css or :match options for a more specific match.

dgmstuart avatar May 10 '16 10:05 dgmstuart

I'm having the same issue when I have the search: true option enabled in combination with other select2 instances on the page (with either multiple: true or not). When the code is looking for the search__field, it finds multiple items because it searches the search field on the whole page, instead of the drop container. I get the following error:

Capybara::Ambiguous:
       Ambiguous match, found 3 elements matching css ".select2-search input.select2-search__field"

I think this could be fixed by seaching for the search__field in the select2-container:

select2_container = find(:css, '#select2_enabled_field_id + .select2-container')
select2_container.find(".select2-selection").click
find('body > .select2-container .select2-search input.select2-search__field').set('search_query')
page.execute_script(%|$("body > .select2-container input.select2-search__field:visible").keyup();|)
drop_container = "body > .select2-container .select2-results"
find(:xpath, "//body").find("#{drop_container} li.select2-results__option[role=treeitem]", text: 'search query').click

Note that I have incorporated the fix of @abuisman for properly selecting the result items when using optgroups: https://github.com/goodwill/capybara-select2/pull/47

OtherCroissant avatar Aug 01 '16 13:08 OtherCroissant

I had the same issue when I had the search: true enabled in conjunction with another select2 element. I got the same error as above:

Capybara::Ambiguous: Ambiguous match, found 2 elements matching css ".select2-search input.select2-search__field"

I got around this by monkey patching the gem and adding in the extra class .select2-container--open to the find as so

find(:xpath, "//body").find(".select2-container--open .select2-search input.select2-search__field").set(value)

This worked a charm for me, and haven't investigated if this extra class will work for different versions of select2 but might help others

mrhillman avatar Mar 01 '18 00:03 mrhillman