{"record":{"id":"12d1c5f87f6e1cde","repo":"SeleniumHQ/selenium","slug":"expected-arr-inspect-to-have-2-elements","errorCode":null,"errorMessage":"expected #{arr.inspect} to have 2 elements","messagePattern":"expected #(.+?) to have 2 elements","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/common/search_context.rb","lineNumber":105,"sourceCode":"      end\n\n      private\n\n      def extract_args(args)\n        case args.size\n        when 2\n          args\n        when 1\n          arg = args.first\n\n          unless arg.respond_to?(:shift)\n            raise ArgumentError,\n                  \"expected #{arg.inspect}:#{arg.class} to respond to #shift\"\n          end\n\n          # this will be a single-entry hash, so use #shift over #first or #[]\n          arr = arg.dup.shift\n          raise ArgumentError, \"expected #{arr.inspect} to have 2 elements\" unless arr.size == 2\n\n          arr\n        else\n          raise ArgumentError, \"wrong number of arguments (#{args.size} for 2)\"\n        end\n      end\n    end # SearchContext\n  end # WebDriver\nend # Selenium\n","sourceCodeStart":87,"sourceCodeEnd":115,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/common/search_context.rb#L87-L115","documentation":"Raised by SearchContext#extract_args when a single Hash argument is passed to find_element/find_elements but the shifted key-value pair does not yield exactly 2 elements. This is an internal consistency check after calling Hash#shift on a duplicated hash; it should always produce a [key, value] pair of size 2. Encountering this error implies the argument was an Array or custom object whose #shift returned something unexpected.","triggerScenarios":"Passing a single Array like find_element([:css]) (size 1) instead of find_element([:css, '#id']) (size 2). Passing a custom object that responds to #shift but returns an array of size != 2. Passing an empty Hash find_element({}) where shift returns nil.","commonSituations":"Constructing a locator array dynamically and accidentally producing a malformed array. Passing a multi-key Hash (only the first key is used via shift, but if values are themselves arrays the size check can be confused).","solutions":["Pass a well-formed two-element array: find_element([:css, '#my-id']).","Or use the Hash form: find_element(css: '#my-id').","Validate the locator structure before passing it if building dynamically."],"exampleFix":"// before\nstrategy = [:css]\nelement = driver.find_element(strategy)  # only 1 element\n\n// after\nstrategy = [:css, '#my-id']\nelement = driver.find_element(strategy)","handlingStrategy":"validation","validationCode":"def build_locator(how, what)\n  [how, what].tap do |arr|\n    raise ArgumentError, 'Locator must have exactly 2 elements' unless arr.size == 2\n  end\nend\n\n# usage: driver.find_element(build_locator(:css, '#id'))","typeGuard":"def valid_two_element_array?(arr)\n  arr.is_a?(Array) && arr.size == 2\nend","tryCatchPattern":"begin\n  driver.find_element(locator)\nrescue ArgumentError => e\n  raise unless e.message.include?('to have 2 elements')\n  raise ArgumentError, \"Locator array malformed: #{locator.inspect}. Expected [strategy, value].\"\nend","preventionTips":["Prefer the Hash form find_element(css: '#id') or two-arg form find_element(:css, '#id') over raw arrays.","When constructing locator arrays programmatically, assert size == 2 before use.","Avoid find_element(*dynamic_array) where dynamic_array could have unexpected sizes."],"tags":["find-element","argument-error","ruby"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}