SeleniumHQ/selenium · error · ArgumentError

invalid value for expiration date: #{obj.inspect}

Error message

invalid value for expiration date: #{obj.inspect}

What it means

The private seconds_from helper converts the :expires cookie option into epoch seconds. It only accepts Time, DateTime, or Numeric values. Any other type (e.g. a String or a Date) raises ArgumentError. This value is then used for the cookie's expiry.

Source

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

      private

      SECONDS_PER_DAY = 86_400.0

      def datetime_at(int)
        DateTime.civil(1970) + (int / SECONDS_PER_DAY)
      end

      def seconds_from(obj)
        case obj
        when Time
          obj.to_f
        when DateTime
          (obj - DateTime.civil(1970)) * SECONDS_PER_DAY
        when Numeric
          obj
        else
          raise ArgumentError, "invalid value for expiration date: #{obj.inspect}"
        end
      end

      def strip_port(str)
        str.split(':', 2).first
      end

      def convert_cookie(cookie)
        {
          name: cookie['name'],
          value: cookie['value'],
          path: cookie['path'],
          domain: cookie['domain'] && strip_port(cookie['domain']),
          expires: cookie['expiry'] && datetime_at(cookie['expiry']),
          same_site: cookie['sameSite'],
          http_only: cookie['httpOnly'],
          secure: cookie['secure']
        }

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Convert date strings to Time before passing: expires: Time.parse('2025-12-31').
  2. Pass epoch seconds directly as a Numeric: expires: 1767225600.
  3. Use DateTime/Time objects (e.g. Time.now + 86400) rather than Date or String.

Example fix

# before
driver.manage.add_cookie(name: 'sid', value: 'x', expires: '2025-12-31')

# after
require 'time'
driver.manage.add_cookie(name: 'sid', value: 'x', expires: Time.parse('2025-12-31'))
Defensive patterns

Strategy: type-guard

Validate before calling

expires = case val
          when Time, DateTime, Numeric then val
          when String then Time.parse(val)
          else raise ArgumentError, 'unsupported expires type'
          end

Type guard

def valid_expires?(obj)
  obj.is_a?(Time) || obj.is_a?(DateTime) || obj.is_a?(Numeric)
end

Try / catch

begin
  driver.manage.add_cookie(name: 'x', value: 'y', expires: val)
rescue ArgumentError => e
  raise unless e.message.include?('expiration date')
  driver.manage.add_cookie(name: 'x', value: 'y', expires: Time.parse(val))
end

Prevention

When it happens

Trigger: Passing expires: '2025-12-31' (a String) to add_cookie. Passing a Date object instead of DateTime/Time. Passing an arbitrary object as :expires.

Common situations: Reading an expiry from JSON/config as a String and passing it directly. Using Date.today instead of Time.now/DateTime.now. Expecting the library to parse date strings.

Related errors


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