SeleniumHQ/selenium · error · ArgumentError

wrong number of arguments (#{args.size} for 2)

Error message

wrong number of arguments (#{args.size} for 2)

What it means

Raised by SearchContext#extract_args when find_element or find_elements is called with 0 arguments or 3+ arguments. The method's case statement handles exactly 1 argument (Hash/Array form) or 2 arguments (how, what form); any other count falls to the else branch.

Source

Thrown at rb/lib/selenium/webdriver/common/search_context.rb:109

      def extract_args(args)
        case args.size
        when 2
          args
        when 1
          arg = args.first

          unless arg.respond_to?(:shift)
            raise ArgumentError,
                  "expected #{arg.inspect}:#{arg.class} to respond to #shift"
          end

          # this will be a single-entry hash, so use #shift over #first or #[]
          arr = arg.dup.shift
          raise ArgumentError, "expected #{arr.inspect} to have 2 elements" unless arr.size == 2

          arr
        else
          raise ArgumentError, "wrong number of arguments (#{args.size} for 2)"
        end
      end
    end # SearchContext
  end # WebDriver
end # Selenium

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pass exactly 1 Hash argument or exactly 2 positional arguments (how, what).
  2. If building arguments dynamically, validate the count before calling find_element.
  3. Use array splat carefully: ensure find_element(*args) produces exactly 1 or 2 elements.

Example fix

// before
element = driver.find_element  # no arguments
element = driver.find_element(:css, '#id', :extra)  # 3 arguments

// after
element = driver.find_element(:css, '#id')
Defensive patterns

Strategy: validation

Validate before calling

def safe_find(driver, *args)
  unless [1, 2].include?(args.size)
    raise ArgumentError, "find_element takes 1 Hash or 2 args (how, what), got #{args.size}"
  end
  driver.find_element(*args)
end

Try / catch

begin
  driver.find_element(*args)
rescue ArgumentError => e
  raise unless e.message.include?('wrong number of arguments')
  raise ArgumentError, "Invalid find_element call with #{args.size} args: #{args.inspect}"
end

Prevention

When it happens

Trigger: Calling find_element() with no arguments. Calling find_element(:css, '#id', 'extra') with a spurious third argument. Calling find_element(a, b, c, d) where multiple locators are accidentally splatted in.

Common situations: Dynamic argument construction via splat (*) that sometimes produces 0 or 3+ args. Refactoring that accidentally drops or duplicates arguments. Calling find_element on an array that gets splatted incorrectly.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/30526772ba62ad32. Report an issue: GitHub.