SeleniumHQ/selenium · error · ArgumentError
Custom page size must include :width and :height
Error message
Custom page size must include :width and :height
What it means
When page_size= is given a Hash, it must contain both :width and :height keys (Symbol keys). If either is missing, ArgumentError is raised. The values represent dimensions in centimeters.
Source
Thrown at rb/lib/selenium/webdriver/common/print_options.rb:83
# 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
- Include both Symbol keys: page_size = { width: 21.0, height: 29.7 }.
- If keys come from JSON/string sources, transform them: hash.transform_keys(&:to_sym).
- Validate value.key?(:width) && value.key?(:height) before assignment.
Example fix
# before
print_options.page_size = { 'width' => 21.0 }
# after
print_options.page_size = { width: 21.0, height: 29.7 } Defensive patterns
Strategy: validation
Validate before calling
raise ArgumentError, 'page size needs :width and :height' unless hash.key?(:width) && hash.key?(:height)
Type guard
def complete_page_hash?(hash) hash.is_a?(Hash) && hash.key?(:width) && hash.key?(:height) end
Try / catch
begin
print_options.page_size = hash
rescue ArgumentError => e
raise unless e.message.include?('width and :height')
print_options.page_size = hash.transform_keys(&:to_sym)
end Prevention
- Use Symbol keys (:width, :height) when building custom sizes.
- transform_keys(&:to_sym) when data originates from JSON/string sources.
- Validate both keys exist before assignment.
When it happens
Trigger: Passing { width: 21.0 } without :height. Using string keys 'width'/'height' instead of Symbols. Passing an incomplete hash built from partial user input.
Common situations: Constructing the hash programmatically and omitting one dimension. Using string keys because the data came from JSON. Copying a partial config.
Related errors
- Invalid page size: #{value}
- Page size must be a Symbol or a Hash
- Value of scale should be between 0.1 and 2
- Orientation value must be one of {self.ORIENTATION_VALUES}
- {property_name} cannot be less than 0
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/cde7b4971e81a747.
Report an issue: GitHub.