{"record":{"id":"25f4d078b86a922b","repo":"teamcapybara/capybara","slug":"item-matched-the-provided-selector","errorCode":null,"errorMessage":"Item matched the provided selector","messagePattern":"Item matched the provided selector","errorType":"exception","errorClass":"Capybara::ExpectationNotMet","httpStatus":null,"severity":"error","filePath":"lib/capybara/node/matchers.rb","lineNumber":604,"sourceCode":"      # @raise [Capybara::ExpectationNotMet]      If the selector does not match\n      #\n      def assert_matches_selector(*args, &optional_filter_block)\n        _verify_match_result(args, optional_filter_block) do |result|\n          raise Capybara::ExpectationNotMet, 'Item does not match the provided selector' unless result.include? self\n        end\n      end\n\n      ##\n      #\n      # Asserts that the current node does not match a given selector.\n      # Usage is identical to {#assert_matches_selector}.\n      #\n      # @param (see #assert_matches_selector)\n      # @raise [Capybara::ExpectationNotMet]      If the selector matches\n      #\n      def assert_not_matches_selector(*args, &optional_filter_block)\n        _verify_match_result(args, optional_filter_block) do |result|\n          raise Capybara::ExpectationNotMet, 'Item matched the provided selector' if result.include? self\n        end\n      end\n\n      ##\n      #\n      # Checks if the current node matches given selector.\n      #\n      # @param (see #has_selector?)\n      # @return [Boolean]\n      #\n      def matches_selector?(*args, **options, &optional_filter_block)\n        make_predicate(options) { assert_matches_selector(*args, options, &optional_filter_block) }\n      end\n\n      ##\n      #\n      # Checks if the current node matches given XPath expression.\n      #","sourceCodeStart":586,"sourceCodeEnd":622,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/node/matchers.rb#L586-L622","documentation":"assert_not_matches_selector is the inverse of assert_matches_selector: it raises ExpectationNotMet 'Item matched the provided selector' when the receiver element DOES match the selector plus filters, which the caller expected it not to. Same MatchQuery machinery (_verify_match_result), inverted condition on result.include?(self).","triggerScenarios":"el.assert_not_matches_selector('li') on an element that is an li; assert_not_matches_selector('a', text: 'Delete') where the text matches exactly; asserting not-checked with assert_not_matches_selector('input', checked: true) while the checkbox is indeed checked; verifying an element lost a class ('.active') while the class is still applied.","commonSituations":"Guard assertions that a node no longer carries a state (active/disabled/selected) that in fact persists, copy changes making text filters match again, class-removal transitions not yet applied at assert time.","solutions":["Check the element's actual state (el[:class], el.checked?) and adjust the expectation or drive the UI to change it first","Add wait for state transitions: assert_not_matches_selector('li', '.active', wait: 3) — note match queries reject count options, not wait","Use exact text if partial matching causes false positives: assert_not_matches_selector('a', text: 'Delete', exact_text: true)","Confirm you are asserting on the intended element instance (re-find after DOM replacement)"],"exampleFix":"# before\nrow.assert_not_matches_selector('tr', '.selected')  # still selected\n\n# after\nclick_link('Unselect')\nrow.assert_not_matches_selector('tr', '.selected', wait: 3)","handlingStrategy":"validation","validationCode":"return if el.matches_selector?('li', '.active') # nothing to assert\nel.assert_not_matches_selector('li', '.active')","typeGuard":"def node_free_of?(node, *args, **opts, &block)\n  node.not_matches_selector?(*args, **opts, &block)\nend","tryCatchPattern":"begin\n  el.assert_not_matches_selector('a', text: 'Delete', exact_text: true, wait: 3)\nrescue Capybara::ExpectationNotMet\n  # node unexpectedly matches — drive UI to clear state, then re-check once\n  el.assert_not_matches_selector('a', text: 'Delete', exact_text: true, wait: 5)\nend","preventionTips":["Use exact_text: true so partial text matches do not trip the negative assertion","Perform the state-clearing action before asserting not-matching","Prefer not_matches_selector? predicate in conditional test flows"],"tags":["capybara","assertion","negative-assertion","match-query","element"],"backgroundTag":"element-selector-match-unexpected","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}