SeleniumHQ/selenium · error · ValueError

{self.name} should be of type {self.expected_type.__name__}

Error message

{self.name} should be of type {self.expected_type.__name__}

What it means

The _ValidateTypeDescriptor validates that the assigned value is an instance of the descriptor's expected_type. For PrintOptions, background and shrink_to_fit require bool, and page_ranges requires list. A wrong type raises ValueError naming the property and the expected type.

Source

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

    def __set__(self, obj, value) -> None:
        if value not in self.ORIENTATION_VALUES:
            raise ValueError(f"Orientation value must be one of {self.ORIENTATION_VALUES}")
        obj._print_options[self.name] = value


class _ValidateTypeDescriptor:
    """Base Class Descriptor which validates type of any subclass attribute."""

    def __init__(self, name, expected_type: type):
        self.name = name
        self.expected_type = expected_type

    def __get__(self, obj, cls):
        return obj._print_options.get(self.name, None)

    def __set__(self, obj, value) -> None:
        if not isinstance(value, self.expected_type):
            raise ValueError(f"{self.name} should be of type {self.expected_type.__name__}")
        obj._print_options[self.name] = value


class _ValidateBackGround(_ValidateTypeDescriptor):
    """Expected type of background attribute."""

    def __init__(self, name):
        super().__init__(name, bool)


class _ValidateShrinkToFit(_ValidateTypeDescriptor):
    """Expected type of shrink to fit attribute."""

    def __init__(self, name):
        super().__init__(name, bool)


class _ValidatePageRanges(_ValidateTypeDescriptor):

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pass a real bool for background/shrink_to_fit and a list for page_ranges.
  2. Convert string config values: background = (val == 'true') or bool(val).
  3. Wrap single ranges in a list: page_ranges = ['1-3'].

Example fix

# before
print_options.background = 'true'      # str, not bool -> ValueError
print_options.page_ranges = '1-3'      # str, not list -> ValueError

# after
print_options.background = True
print_options.page_ranges = ['1-3']
Defensive patterns

Strategy: type-guard

Validate before calling

if 'background' in cfg:
    assert isinstance(cfg['background'], bool), 'background must be bool'
if 'pageRanges' in cfg:
    assert isinstance(cfg['pageRanges'], list), 'pageRanges must be list'

Type guard

def is_bool(v) -> bool:
    return isinstance(v, bool)
def is_str_list(v) -> bool:
    return isinstance(v, list) and all(isinstance(x, str) for x in v)

Try / catch

try:
    print_options.background = bg
except ValueError:
    print_options.background = bool(bg) if not isinstance(bg, bool) else bg

Prevention

When it happens

Trigger: Setting print_options.background = 'true' (string instead of bool), print_options.background = 1 (int), print_options.shrink_to_fit = 'yes', or print_options.page_ranges = '1-3' (string instead of list).

Common situations: Loading config from JSON/env where booleans arrive as strings. Passing a single page-range string instead of a list. Treating Python truthiness as a bool substitute.

Related errors


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