Patchright Python

repository·main·Indexed 23 days ago

https://github.com/kaliiiiiiiiii-vinyzu/patchright-python

A patched, undetected version of the Playwright testing and automation framework designed as a drop-in replacement for Chromium-based browsers. It aims to bypass advanced bot detection systems like Cloudflare, Akamai, and Datadome by applying anti-detection patches to the Playwright/Chromium stack, including the removal of automation flags and the use of isolated execution contexts.

Tokens
1.6K
Snippets
7
Records
8
Agent score
31%

What's inside patchright-python

  1. Understand Patchright's anti-detection patches

    main

    Patchright applies several patches to the Playwright/Chromium stack to avoid common detection vectors:

    • Runtime.enable Leak: Avoids using Runtime.enable by executing JavaScript in isolated ExecutionContexts.
    • Console.enable Leak: Disables the Console API entirely to prevent detection. Note that this means standard console functionality will not work.
    • Command Flags Leaks: Tweaks default arguments to avoid detection. Specifically:
      • Adds --disable-blink-features=AutomationControlled to avoid navigator.webdriver detection.
      • Removes --enable-automation to avoid navigator.webdriver detection.
      • Removes --disable-popup-blocking, --disable-component-update, --disable-default-apps, and --disable-extensions to avoid various stealth driver and feature-based detections.
    • Closed Shadow Roots: Patchright allows interacting with elements inside Closed Shadow Roots using standard locators and XPaths.
  2. Best Practice: Use Chrome without Fingerprint Injection for maximum undetectability

    main

    To achieve the highest level of undetectability, it is recommended to use Google Chrome instead of Chromium and avoid manual fingerprint injection.

    1. Install Chrome via: patchright install chrome.
    2. Use launch_persistent_context with channel="chrome".
    3. Crucial: Do NOT add custom browser headers or a custom user_agent, as these can be detected.

    Recommended configuration:

    playwright.chromium.launch_persistent_context(
        user_data_dir="...",
        channel="chrome",
        headless=False,
        no_viewport=True,
        # do NOT add custom browser headers or user_agent
        ...
    )
  3. Use Patchright as a drop-in replacement for Playwright

    main

    Patchright is designed to be a drop-in replacement for Playwright. You can use either the synchronous or asynchronous API by importing from patchright.sync_api or patchright.async_api respectively.

    # Synchronous usage
    from patchright.sync_api import sync_playwright
    
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page()
        page.goto('http://playwright.dev')
        page.screenshot(path=f'example-{p.chromium.name}.png')
        browser.close()
  4. Install Patchright Python

    main

    Install the patchright package from PyPI using pip, then install the necessary Chromium driver.

    Note: Patchright only supports Chromium-based browsers. Firefox and Webkit are not supported.

    # Install Patchright with Pip from PyPI
    pip install patchright
    
    # Install Chromium-Driver for Patchright
    patchright install chromium
  5. Use Patchright with Asyncio

    main

    For asynchronous workflows, import from patchright.async_api and use async with context managers.

    import asyncio
    # patchright here!
    from patchright.async_api import async_playwright
    
    async def main():
        async with async_playwright() as p:
            browser = await p.chromium.launch()
            page = await browser.new_page()
            await page.goto('http://playwright.dev')
            await page.screenshot(path=f'example-{p.chromium.name}.png')
            await browser.close()
    
    asyncio.run(main())
  6. Use `isolated_context` with `evaluate_all` method

    main

    Patchright extends the Locator.evaluate_all method by adding an isolated_context parameter. This allows you to choose the Execution Context (Main or Isolated) for the JavaScript expression execution. By default, isolated_context is set to True (Isolated context).

    # Example of the extended signature for Locator.evaluate_all
    Locator.evaluate_all(
        expression: str,
        arg: typing.Optional[typing.Any] = None,
        ...,
        isolated_context: typing.Optional[bool] = True
    )
  7. Use `isolated_context` with `evaluate` methods

    main

    Patchright extends the standard Playwright evaluate family of methods by adding an isolated_context parameter. This allows you to choose the Execution Context (Main or Isolated) for the JavaScript expression execution. By default, isolated_context is set to True (Isolated context).

    # Example of the extended signature for evaluate methods
    # Supported on: Frame, Page, Locator, Worker, and JSHandle
    object.evaluate(
        expression: str,
        arg: typing.Optional[typing.Any] = None,
        ...,
        isolated_context: typing.Optional[bool] = True
    )
  8. Use `isolated_context` with `evaluate_handle` methods

    main

    Patchright extends the standard Playwright evaluate_handle family of methods by adding an isolated_context parameter. This allows you to choose the Execution Context (Main or Isolated) for the JavaScript expression execution. By default, isolated_context is set to True (Isolated context).

    # Example of the extended signature for evaluate_handle methods
    # Supported on: Frame, Page, Locator, Worker, and JSHandle
    object.evaluate_handle(
        expression: str,
        arg: typing.Optional[typing.Any] = None,
        ...,
        isolated_context: typing.Optional[bool] = True
    )