SeleniumHQ/selenium · error · ArgumentError

name is required

Error message

name is required

What it means

Manager#add_cookie requires a :name key in the options hash; a cookie without a name is invalid per the WebDriver specification. The method raises ArgumentError immediately if opts[:name] is missing or nil. A :value is also required (checked next).

Source

Thrown at rb/lib/selenium/webdriver/common/manager.rb:46

        @bridge = bridge
      end

      #
      # Add a cookie to the browser
      #
      # @param [Hash] opts the options to create a cookie with.
      # @option opts [String] :name A name
      # @option opts [String] :value A value
      # @option opts [String] :path ('/') A path
      # @option opts [String] :secure (false) A boolean
      # @option opts [String] :same_site (Strict or Lax) currently supported only in chrome 80+ versions
      # @option opts [Time,DateTime,Numeric,nil] :expires (nil) Expiry date, either as a Time, DateTime, or seconds since epoch.
      #
      # @raise [ArgumentError] if :name or :value is not specified
      #

      def add_cookie(opts = {})
        raise ArgumentError, 'name is required' unless opts[:name]
        raise ArgumentError, 'value is required' unless opts[:value]

        # NOTE: This is required because of https://bugs.chromium.org/p/chromedriver/issues/detail?id=3732
        opts[:secure] ||= false

        same_site = opts.delete(:same_site)
        opts[:sameSite] = same_site if same_site

        http_only = opts.delete(:http_only)
        opts[:httpOnly] = http_only if http_only

        obj = opts.delete(:expires)
        opts[:expiry] = seconds_from(obj).to_int if obj

        @bridge.add_cookie opts
      end

      #

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pass a Hash with both Symbol keys: driver.manage.add_cookie(name: 'sid', value: 'abc').
  2. If building the hash dynamically, ensure the :name key is present and non-nil before calling add_cookie.
  3. Use Symbol keys (:name, :value) rather than string keys to match the documented option names.

Example fix

# before
driver.manage.add_cookie(value: 'abc')

# after
driver.manage.add_cookie(name: 'sid', value: 'abc')
Defensive patterns

Strategy: validation

Validate before calling

raise ArgumentError, 'cookie :name is required' unless cookie_hash.key?(:name) && cookie_hash[:name]

Type guard

def valid_cookie?(hash)
  hash.is_a?(Hash) && !hash[:name].nil? && !hash[:value].nil?
end

Try / catch

begin
  driver.manage.add_cookie(opts)
rescue ArgumentError => e
  raise unless e.message.include?('name is required')
  opts = opts.merge(name: default_name)
  driver.manage.add_cookie(opts)
end

Prevention

When it happens

Trigger: Calling driver.manage.add_cookie(value: 'x') with no :name. Passing positional arguments like add_cookie('name', 'value') instead of a hash. Using string keys 'name' that do not match the expected Symbol :name in some code paths.

Common situations: Forgetting the hash key and passing values positionally. Building the cookie hash dynamically and the name key is nil/absent. Using a string key where the lookup uses opts[:name] (Symbol).

Related errors


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