{"record":{"id":"d2f05fdf19f1a7ab","repo":"teamcapybara/capybara","slug":"xpath-expressions-are-not-supported-for-the-att","errorCode":null,"errorMessage":"XPath expressions are not supported for the :#{attribute} filter with CSS based selectors","messagePattern":"XPath expressions are not supported for the :#(.+?) filter with CSS based selectors","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/capybara/selector/builders/css_builder.rb","lineNumber":47,"sourceCode":"          end.join(', ')\n        end\n      end\n\n    private\n\n      def regexp_conditions(name, value)\n        Selector::RegexpDisassembler.new(value).alternated_substrings.map do |strs|\n          strs.map do |str|\n            \"[#{name}*='#{str}'#{' i' if value.casefold?}]\"\n          end.join\n        end\n      end\n\n      def attribute_conditions(attributes)\n        attributes.map do |attribute, value|\n          case value\n          when XPath::Expression\n            raise ArgumentError, \"XPath expressions are not supported for the :#{attribute} filter with CSS based selectors\"\n          when Regexp\n            Selector::RegexpDisassembler.new(value).substrings.map do |str|\n              \"[#{attribute}*='#{str}'#{' i' if value.casefold?}]\"\n            end.join\n          when true\n            \"[#{attribute}]\"\n          when false\n            ':not([attribute])'\n          else\n            if attribute == :id\n              \"##{::Capybara::Selector::CSS.escape(value)}\"\n            else\n              \"[#{attribute}='#{value}']\"\n            end\n          end\n        end.join\n      end\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/selector/builders/css_builder.rb#L29-L65","documentation":"When a selector is resolved with CSS (CSS-format selector or the CSS builder), attribute filter conditions must be rendered as CSS attribute selectors. A value that is an XPath::Expression (from the xpath gem, e.g. XPath.contains, XPath.anywhere) cannot be translated to CSS, so Capybara::Selector::Builders::CSSBuilder#attribute_conditions raises ArgumentError naming the offending filter. String and Regexp values are fine; XPath expressions require the XPath side.","triggerScenarios":"find(:css, 'input', id: XPath.contains('user_id')); find(:css, 'div', style: XPath.contains('display')); a custom selector defined with format :css whose filter receives XPath expressions from callers - the builder reaches attribute_conditions and fails.","commonSituations":"Reusing idioms from Capybara's own XPath-based selector definitions in option hashes for CSS-based queries; writing generic helper methods that accept either strings or XPath fragments and passing them through to CSS queries; custom CSS-format selectors whose API suggests XPath composability.","solutions":["Pass String or Regexp values for CSS-based queries: id: /user_id/ or id: 'exact_id' instead of an XPath expression.","Move the condition into an XPath-format query: find(:xpath, XPath.css('input')[XPath.attr(:id).contains('user_id')]).","For custom selectors that need composable conditions, define/register the selector with XPath format instead of CSS.","In shared helpers, convert XPath fragments to regexps (or route to :xpath) when the target selector is CSS-based."],"exampleFix":"# before\nfind(:css, 'input', id: XPath.contains('user_id'))\n\n# after\nfind(:css, 'input', id: /user_id/)","handlingStrategy":"type-guard","validationCode":"value = XPath.contains('user_id')\nraise ArgumentError, 'XPath expressions need an :xpath query' if value.is_a?(XPath::Expression)\nfind(:css, 'input', id: /user_id/)","typeGuard":"def css_filter_value?(value)\n  value.is_a?(String) || value.is_a?(Regexp) || value.is_a?(Array) || [true, false].include?(value)\nend","tryCatchPattern":null,"preventionTips":["In generic helpers, accept only strings/regexps for CSS-based selectors and translate XPath fragments to regexps yourself.","Route composable XPath conditions to find(:xpath, XPath.css(...)[...]) rather than option hashes."],"tags":["ruby","capybara","css","xpath","selector-builders"],"backgroundTag":"xpath-css-selector-mismatch","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}