SeleniumHQ/selenium · error · ValueError

{property_name} should be an integer or a float

Error message

{property_name} should be an integer or a float

What it means

The _validate_num_property helper checks that a numeric property (page height/width, margins, scale) is an int or float before accepting it. Passing a string, None, or any non-numeric type raises ValueError naming the property. This is the type gate that runs before the range check.

Source

Thrown at py/selenium/webdriver/common/print_page_options.py:361

        Args:
            page_size: A dictionary containing 'height' and 'width' keys with
                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

  1. Cast values to float before assigning: float(value).
  2. Strip units from strings before conversion.

Example fix

# before
print_options.set_page_size({'height': '29.7', 'width': '21.0'})  # str -> ValueError

# after
print_options.set_page_size({'height': float('29.7'), 'width': float('21.0')})
Defensive patterns

Strategy: validation

Validate before calling

def to_number(v):
    n = float(v)
    return n
h = to_number(page_size['height']); w = to_number(page_size['width'])
print_options.set_page_size({'height': h, 'width': w})

Type guard

def is_number(v) -> bool:
    return isinstance(v, (int, float)) and not isinstance(v, bool)

Try / catch

try:
    print_options.set_page_size(page_size)
except ValueError:
    page_size = {k: float(v) for k, v in page_size.items()}
    print_options.set_page_size(page_size)

Prevention

When it happens

Trigger: Calling print_options.set_page_size({'height': '29.7', 'width': 21.0}), setting page_height = '10', or set_page_size with string values from config. scale = '1.0' also hits this via the ScaleDescriptor.

Common situations: Reading dimensions from JSON/env as strings. Passing measurements with units ('29.7cm'). Forgetting to cast form/config input.

Related errors


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