{"id":3384,"library":"patchright","title":"Patchright","description":"Patchright is an undetected Python version of the Playwright testing and automation library (version 1.58.2). It serves as a drop-in replacement for Playwright, specifically designed to bypass modern anti-bot detection systems by patching Chrome DevTools Protocol (CDP) leaks and browser fingerprinting issues. The library is actively maintained and receives updates to combat evolving detection techniques.","status":"active","version":"1.58.2","language":"en","source_language":"en","source_url":"https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python","tags":["web scraping","automation","playwright","undetected browser","anti-bot bypass","chromium"],"install":[{"cmd":"pip install patchright","lang":"bash","label":"Install Patchright"},{"cmd":"patchright install chrome","lang":"bash","label":"Install Google Chrome (recommended for stealth)"}],"dependencies":[],"imports":[{"note":"To utilize Patchright's stealth features, import directly from `patchright.sync_api` instead of `playwright.sync_api`.","wrong":"from playwright.sync_api import sync_playwright","symbol":"sync_playwright","correct":"from patchright.sync_api import sync_playwright"},{"note":"For asynchronous operations with Patchright's stealth, import from `patchright.async_api`.","wrong":"from playwright.async_api import async_playwright","symbol":"async_playwright","correct":"from patchright.async_api import async_playwright"}],"quickstart":{"code":"import os\nimport asyncio\nfrom patchright.sync_api import sync_playwright\n\ndef run_sync_example():\n    with sync_playwright() as p:\n        # Recommended configuration for maximum undetectability:\n        # Use launch_persistent_context with channel='chrome' and headless=False\n        # viewport=null (or no_viewport=True) prevents setting a fixed viewport, aiding stealth.\n        # user_data_dir is crucial for persisting cookies/session info if solving captchas manually.\n        user_data_dir = os.environ.get('PATCHRIGHT_USER_DATA_DIR', '/tmp/patchright_profile')\n        browser = p.chromium.launch_persistent_context(\n            user_data_dir=user_data_dir,\n            channel=\"chrome\",\n            headless=False,\n            viewport=None, # equivalent to no_viewport=True\n            # Do NOT add custom browser headers or user_agent here, Patchright handles it.\n        )\n        page = browser.new_page()\n        try:\n            print(f\"Navigating to example.com with {p.chromium.name}...\")\n            page.goto(\"https://example.com\")\n            print(f\"Page title: {page.title()}\")\n            # Example interaction: get text from an element\n            text = page.locator('h1').inner_text()\n            print(f\"H1 text: {text}\")\n            page.screenshot(path=f\"example-{p.chromium.name}.png\")\n            print(\"Screenshot saved.\")\n        finally:\n            browser.close()\n            print(\"Browser closed.\")\n\nasync def run_async_example():\n    async with async_playwright() as p:\n        user_data_dir = os.environ.get('PATCHRIGHT_USER_DATA_DIR_ASYNC', '/tmp/patchright_profile_async')\n        browser = await p.chromium.launch_persistent_context(\n            user_data_dir=user_data_dir,\n            channel=\"chrome\",\n            headless=False,\n            viewport=None\n        )\n        page = await browser.new_page()\n        try:\n            print(f\"Navigating to example.com (async) with {p.chromium.name}...\")\n            await page.goto(\"https://example.com\")\n            print(f\"Page title (async): {await page.title()}\")\n            text = await page.locator('h1').inner_text()\n            print(f\"H1 text (async): {text}\")\n            await page.screenshot(path=f\"example-async-{p.chromium.name}.png\")\n            print(\"Screenshot saved (async).\")\n        finally:\n            await browser.close()\n            print(\"Browser closed (async).\")\n\nif __name__ == \"__main__\":\n    print(\"--- Running Synchronous Example ---\")\n    run_sync_example()\n    print(\"\\n--- Running Asynchronous Example ---\")\n    # Make sure to install browsers: `patchright install chrome`\n    asyncio.run(run_async_example())\n","lang":"python","description":"This quickstart demonstrates both synchronous and asynchronous usage of Patchright, highlighting the recommended configuration for maximum undetectability. It launches a persistent Chrome browser in non-headless mode, navigates to example.com, captures the page title and an H1 element's text, and takes a screenshot. The `user_data_dir` is made configurable via an environment variable for testing persistence. Remember to run `patchright install chrome` before executing."},"warnings":[{"fix":"Ensure your automation targets Chromium-based browsers and use `p.chromium.launch()` or `p.chromium.launch_persistent_context()`.","message":"Patchright exclusively supports Chromium-based browsers (like Google Chrome, Microsoft Edge, Opera). Firefox and WebKit-based browsers are not supported, as the patching techniques are specific to the Blink engine.","severity":"breaking","affected_versions":"All versions"},{"fix":"If logging is required, use JavaScript logger libraries that do not rely on the browser's native `console` object, or rely on Python-side logging for your automation script.","message":"The Console API is completely disabled in Patchright to prevent detection via `Console.enable` leaks. `console.log()` and other console functionalities will not work.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Set `headless=False` (recommended), use `viewport=None`, and let Patchright handle user agents and default browser headers for optimal stealth. Use `channel=\"chrome\"` with a separate Chrome installation for best results.","message":"For maximum undetectability, avoid running Patchright in headless mode (`headless=True`) and refrain from adding custom user agents or extra HTTP headers in your launch options. Patchright applies specific tweaks to these by default.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of potential conflicts if your Playwright script heavily utilizes custom routing. Test thoroughly and report any specific issues to the Patchright maintainers; some underlying issues might be Playwright-level.","message":"Patchright's use of Playwright Routes to inject JavaScript for InitScripts (to avoid `Runtime.enable` leaks) *may* cause unexpected bugs in other parts of your code that rely on Playwright Routes functionality.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}