SeleniumHQ/selenium · error · ValueError
{property_name} cannot be less than 0
Error message
{property_name} cannot be less than 0 What it means
After confirming a value is numeric, _validate_num_property rejects any value below zero. Page height/width, margins, and scale must be non-negative. A negative number raises ValueError naming the property. (Scale has its own narrower 0.1–2.0 range check that fires after this for the scale descriptor.)
Source
Thrown at py/selenium/webdriver/common/print_page_options.py:364
respective values in cm.
Example:
self.set_page_size(PageSize.A4) # A4 predefined size
self.set_page_size({"height": 15.0, "width": 20.0}) # Custom size
"""
self._validate_num_property("height", page_size["height"])
self._validate_num_property("width", page_size["width"])
self._page["height"] = page_size["height"]
self._page["width"] = page_size["width"]
self._print_options["page"] = self._page
def _validate_num_property(self, property_name: str, value: float) -> None:
"""Helper function to validate some of the properties."""
if not isinstance(value, (int, float)):
raise ValueError(f"{property_name} should be an integer or a float")
if value < 0:
raise ValueError(f"{property_name} cannot be less than 0")
View on GitHub (pinned to aa36b38e69)
Solutions
- Use 0 or a positive value; use None/omission for 'unset'.
- Clamp negative inputs to 0 or skip the assignment when the value is negative.
Example fix
# before
print_options.set_page_size({'height': -1, 'width': 21}) # -1 sentinel -> ValueError
# after
if h is not None and h >= 0:
print_options.set_page_size({'height': h, 'width': w}) Defensive patterns
Strategy: validation
Validate before calling
if not isinstance(value, (int, float)) or value < 0:
raise ValueError(f'{name} must be a non-negative number')
# then set Type guard
def is_non_negative_number(v) -> bool:
return isinstance(v, (int, float)) and not isinstance(v, bool) and v >= 0 Try / catch
try:
print_options.set_page_size(page_size)
except ValueError:
page_size = {k: max(0.0, float(v)) for k, v in page_size.items()}
print_options.set_page_size(page_size) Prevention
- Do not use -1 as an 'unset' sentinel; use None and omit the call.
- Clamp computed dimensions to >= 0.
When it happens
Trigger: Setting print_options.set_page_size({'height': -5, 'width': 21}), page_height = -1.0, or margin_top = -2. Passing a sentinel like -1 meaning 'unset'.
Common situations: Using -1 as an 'unset' marker from config. Sign errors in calculations. Defaulting missing values to -1.
Related errors
- Value of scale should be between 0.1 and 2
- Orientation value must be one of {self.ORIENTATION_VALUES}
- {property_name} should be an integer or a float
- Invalid page size: #{value}
- Custom page size must include :width and :height
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/2a22dff72aea8ec4.
Report an issue: GitHub.