SeleniumHQ/selenium · error · ArgumentError

Invalid page size: #{value}

Error message

Invalid page size: #{value}

What it means

PrintOptions#page_size= accepts a Symbol naming a predefined paper size (:letter, :legal, :a4, :tabloid). If the Symbol is not one of those keys, ArgumentError is raised. For custom dimensions, pass a Hash with :width and :height instead.

Source

Thrown at rb/lib/selenium/webdriver/common/print_options.rb:78

      # Gets the current page size.
      #
      # @return [Hash] The current page size hash with :width and :height.
      attr_reader :page_size

      # Sets the page size. Can be a predefined symbol or custom size hash.
      #
      # @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.
      def page_size=(value)
        predefined_sizes = {
          letter: {width: 21.59, height: 27.94},
          legal: {width: 21.59, height: 35.56},
          a4: {width: 21.0, height: 29.7},
          tabloid: {width: 27.94, height: 43.18}
        }

        case value
        when Symbol
          raise ArgumentError, "Invalid page size: #{value}" unless predefined_sizes.key?(value)

          @page_size = predefined_sizes[value]
        when Hash
          unless value.key?(:width) && value.key?(:height)
            raise ArgumentError, 'Custom page size must include :width and :height'
          end

          @page_size = value
        else
          raise ArgumentError, 'Page size must be a Symbol or a Hash'
        end
      end
    end
  end
end

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Use one of the predefined Symbols: :letter, :legal, :a4, or :tabloid.
  2. For any other size, pass a custom Hash: page_size = { width: 29.7, height: 42.0 } (cm).
  3. Double-check Symbol casing; the keys are lowercase (:a4, not :A4).

Example fix

# before
print_options.page_size = :a3

# after
print_options.page_size = { width: 29.7, height: 42.0 }
Defensive patterns

Strategy: validation

Validate before calling

allowed = %i[letter legal a4 tabloid]
raise ArgumentError, "unsupported size #{size}" unless allowed.include?(size)

Type guard

def predefined_size?(sym)
  sym.is_a?(Symbol) && %i[letter legal a4 tabloid].include?(sym)
end

Try / catch

begin
  print_options.page_size = size
rescue ArgumentError => e
  raise unless e.message.include?('Invalid page size')
  print_options.page_size = :a4
end

Prevention

When it happens

Trigger: Calling print_options.page_size = :a3 (not predefined). Using an uppercase or misspelled Symbol like :A4 or :leter. Passing a Symbol for a paper size the library does not ship.

Common situations: Assuming a paper size is supported when it is not. Case sensitivity mistakes (:A4 vs :a4). Typos in size names.

Related errors


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