{"record":{"id":"c522318282a9438c","repo":"teamcapybara/capybara","slug":"ambiguous-match-found-result-size-elements-mat","errorCode":null,"errorMessage":"Ambiguous match, found #{result.size} elements matching #{query.applied_description}","messagePattern":"Ambiguous match, found #(.+?) elements matching #(.+?)","errorType":"exception","errorClass":"Capybara::Ambiguous","httpStatus":null,"severity":"error","filePath":"lib/capybara/node/finders.rb","lineNumber":310,"sourceCode":"      #\n      def first(*args, **options, &optional_filter_block)\n        options = { minimum: 1 }.merge(options) unless options_include_minimum?(options)\n        all(*args, **options, &optional_filter_block).first\n      end\n\n    private\n\n      def synced_resolve(query)\n        synchronize(query.wait) do\n          if prefer_exact?(query)\n            result = query.resolve_for(self, true)\n            result = query.resolve_for(self, false) if result.empty? && query.supports_exact? && !query.exact?\n          else\n            result = query.resolve_for(self)\n          end\n\n          if ambiguous?(query, result)\n            raise Capybara::Ambiguous, \"Ambiguous match, found #{result.size} elements matching #{query.applied_description}\"\n          end\n          raise Capybara::ElementNotFound, \"Unable to find #{query.applied_description}\" if result.empty?\n\n          result.first\n        end.tap(&:allow_reload!)\n      end\n\n      def ambiguous?(query, result)\n        %i[one smart].include?(query.match) && (result.size > 1)\n      end\n\n      def prefer_exact?(query)\n        %i[smart prefer_exact].include?(query.match)\n      end\n\n      def options_include_minimum?(opts)\n        %i[count minimum between].any? { |key| opts.key?(key) }\n      end","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/node/finders.rb#L292-L328","documentation":"Finders#find (via synced_resolve) raises Capybara::Ambiguous when more than one element matches and the query's match mode is :one or :smart (smart is the default). With :smart, find first tries an exact locator match and only falls back to partial matching, so the error means even the exact pass returned multiple elements. It is the counterpart of ElementNotFound: one locator must resolve to exactly one element.","triggerScenarios":"find('a', text: 'Delete') on a page with two 'Delete' links (smart mode tries exact text first, still finds 2); find('li') on a multi-item list; find('button') with multiple buttons under match: :one; find(field: 'Name') when both a label and an aria-label produce separate matches.","commonSituations":"Repeated UI components (rows each containing an Edit button), icons with identical accessible names, test data duplicated in the DOM, switching default_match to :one globally and having previously-tolerated multiple matches now fail.","solutions":["Scope the search to a unique container: find('#user-1').find('a', text: 'Delete')","Make the locator exact and unique: find('a.exact-class', text: 'Delete', exact_text: true)","Accept the first match explicitly: find('a', text: 'Delete', match: :first) or all('a', text: 'Delete').first","Pick by index/attributes only when order is stable: all('a', text: 'Delete')[1]"],"exampleFix":"# before\nfind('a', text: 'Delete').click  # two Delete links exist\n\n# after\nfind('#row-42').find('a', text: 'Delete').click\n# or\nall('a', text: 'Delete').first.click","handlingStrategy":"validation","validationCode":"matches = page.all('a', text: 'Delete')\nraise 'ambiguous: scope the selector' if matches.size > 1\nmatches.first&.click","typeGuard":"def unique_match?(node, *args, **opts, &block)\n  node.all(*args, **opts, &block).size == 1\nend","tryCatchPattern":"begin\n  find('a', text: 'Delete')\nrescue Capybara::Ambiguous => e\n  all('a', text: 'Delete').first # deliberate first-match policy\nend","preventionTips":["Scope find calls inside unique containers (within('#row-42') { ... })","Make locators specific enough to be unique: ids, data attributes, exact classes","Only opt into match: :first when element order is guaranteed by the app"],"tags":["capybara","ambiguous","find","selector","match-mode"],"backgroundTag":"ambiguous-selector-match","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}