{"record":{"id":"d9eda1b42bb12988","repo":"D4Vinci/Scrapling","slug":"failed-to-capture-screenshot-for-url","errorCode":null,"errorMessage":"Failed to capture screenshot for {url}","messagePattern":"Failed to capture screenshot for (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"scrapling/core/ai.py","lineNumber":375,"sourceCode":"                captured[\"bytes\"] = await page.screenshot(**screenshot_kwargs)\n                captured[\"url\"] = page.url\n            except Exception as exc:\n                captured[\"error\"] = exc\n\n        await entry.session.fetch(\n            url,\n            wait=wait,\n            timeout=timeout,\n            network_idle=network_idle,\n            wait_selector=wait_selector,\n            wait_selector_state=wait_selector_state,\n            page_action=_capture,\n        )\n\n        if \"error\" in captured:\n            raise captured[\"error\"]\n        if \"bytes\" not in captured:\n            raise RuntimeError(f\"Failed to capture screenshot for {url}\")\n\n        image = Image(data=captured[\"bytes\"], format=image_type).to_image_content()\n        return [image, TextContent(type=\"text\", text=captured[\"url\"])]\n\n    @staticmethod\n    async def get(\n        url: str,\n        impersonate: ImpersonateType = \"chrome\",\n        extraction_type: extraction_types = \"markdown\",\n        css_selector: Optional[str] = None,\n        main_content_only: bool = True,\n        params: Optional[Dict] = None,\n        headers: Optional[Mapping[str, Optional[str]]] = None,\n        cookies: Optional[Dict[str, str]] = None,\n        timeout: Optional[int | float] = 30,\n        follow_redirects: FollowRedirects = \"safe\",\n        max_redirects: int = 30,\n        retries: Optional[int] = 3,","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/D4Vinci/Scrapling/blob/5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f/scrapling/core/ai.py#L357-L393","documentation":"After running the capture callback inside the page, the tool raises RuntimeError('Failed to capture screenshot for {url}') when no bytes were produced and no explicit error was captured — the page action completed without delivering a screenshot and without raising anything catchable. It is the sentinel for 'the navigation/wait succeeded but page.screenshot returned nothing'.","triggerScenarios":"The _capture coroutine never assigning captured['bytes'] — e.g. the page_action short-circuited because navigation was blocked, the page closed mid-flight, or screenshot kwargs were rejected by the Playwright version in use. It follows a successful _navigate_and_act call, so wait/timeout errors usually surface earlier.","commonSituations":"Pages that destroy/close themselves on load (about:blank redirects, anti-bot tricks), headless environments where screenshot is interrupted, or Playwright/Scrapling version mismatches changing screenshot kwarg behavior.","solutions":["Retry the capture once — transient page-close races are the most common cause","Add wait/wait_selector so the page is settled before capture, and network_idle=True for late-loading pages","Check the page isn't redirecting to about:blank or being closed by the site; try block_webrtc/hide_canvas or a stealthy session for anti-bot targets","Verify Playwright browser binaries are installed and versions match (`playwright install chromium`)"],"exampleFix":"# before\nimgs = await capture_screenshot(session_id=sid, url=url)\n\n# after\nimgs = await capture_screenshot(\n    session_id=sid, url=url,\n    wait=1500, network_idle=True,  # let page settle\n)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed\n\n@retry(retry=retry_if_exception_type(RuntimeError), stop=stop_after_attempt(2), wait=wait_fixed(1_000), reraise=True)\nasync def safe_capture(**kw):\n    return await capture_screenshot(**kw)","preventionTips":["Set wait/network_idle/wait_selector so pages settle before capture","Keep browser binaries healthy (playwright install) and containers resourced","Retry once on this RuntimeError — page-close races are transient"],"tags":["ai","mcp","screenshot","browser","runtimeerror"],"backgroundTag":null,"analyzedSha":"5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f","analyzedAt":"2026-08-14T22:23:09.440Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}