D4Vinci/Scrapling · error · TypeError

page_action must be callable, got {type(self.page_action).__

Error message

page_action must be callable, got {type(self.page_action).__name__}

What it means

PlaywrightConfig.__post_init__ (msgspec Struct validation) throws TypeError when the `page_action` parameter is truthy but not callable. page_action is meant to be a user-supplied callback (sync: def page_action(page), async: async def) executed on the page after load.

Source

Thrown at scrapling/engines/_browsers/_validators.py:99

    selector_config: Optional[Dict] = {}
    additional_args: Optional[Dict] = {}
    locale: str | None = None
    real_chrome: bool = False
    cdp_url: Optional[str] = None
    useragent: Optional[str] = None
    extra_flags: Optional[List[str]] = None
    blocked_domains: Optional[Set[str]] = None
    block_ads: bool = False
    retries: RetriesCount = 3
    retry_delay: Seconds = 1
    capture_xhr: str | None = None
    executable_path: Optional[str] = None
    dns_over_https: bool = False

    def __post_init__(self):  # pragma: no cover
        """Custom validation after msgspec validation"""
        if self.page_action and not callable(self.page_action):
            raise TypeError(f"page_action must be callable, got {type(self.page_action).__name__}")
        if self.page_setup and not callable(self.page_setup):
            raise TypeError(f"page_setup must be callable, got {type(self.page_setup).__name__}")
        if self.proxy and self.proxy_rotator:
            raise ValueError(
                "Cannot use 'proxy_rotator' together with 'proxy'. "
                "Use either a static proxy or proxy rotation, not both."
            )
        if self.proxy:
            self.proxy = construct_proxy_dict(self.proxy)
        if self.cdp_url:
            cdp_msg = _is_invalid_cdp_url(self.cdp_url)
            if cdp_msg:
                raise ValueError(cdp_msg)

        if not self.cookies:
            self.cookies = []
        if not self.extra_flags:
            self.extra_flags = []

View on GitHub (pinned to 5d213a2d47)

Solutions

  1. Pass the function reference itself: page_action=my_func
  2. If loading from config, resolve the string to an actual function via a registry/dict lookup before passing
  3. For async sessions pass a coroutine function (async def)

Example fix

// before
session.fetch(url, page_action='scroll_to_bottom')

// after
def scroll_to_bottom(page):
    page.mouse.wheel(0, 5000)
session.fetch(url, page_action=scroll_to_bottom)
Defensive patterns

Strategy: type-guard

Validate before calling

if page_action is not None and not callable(page_action):
    raise TypeError('page_action must be callable')
session.fetch(url, page_action=page_action)

Type guard

def is_valid_page_action(fn) -> bool:
    return fn is None or callable(fn)

Try / catch

try:
    session.fetch(url, page_action=cb)
except TypeError as e:
    if 'page_action must be callable' in str(e):
        cb = resolve_callback(cb); session.fetch(url, page_action=cb)
    else:
        raise

Prevention

When it happens

Trigger: Passing a function name as a string (page_action='scroll_page'), passing the result of a call page_action=my_func() instead of the reference, or passing an arbitrary object.

Common situations: Loading callback names from JSON/YAML config files, templating configs, or copy-pasting examples that quote the function.

Related errors


AI-assisted analysis of D4Vinci/Scrapling@5d213a2d47 (2026-08-14). Data as JSON: /api/errors/28281855e731db63. Report an issue: GitHub.