Skip to main content

Make your playwright instance stealthy

Project description

playwright_stealth

Fork of AtuboDad's port of puppeteer-extra-plugin-stealth, with some improvements. Don't expect this to bypass anything but the simplest of bot detection methods. Consider this a proof-of-concept starting point.

I've merged some of the outstanding PRs, added some features, and cleaned up the API surface. See the changelog. The latest major version includes breaking changes.

Install

Install from PyPi:

$ pip install playwright-stealth

Example Usage

Recommended Usage

import asyncio
from playwright.async_api import async_playwright
from playwright_stealth import Stealth


async def main():
    # This is the recommended usage. All pages created will have stealth applied:
    async with Stealth().use_async(async_playwright()) as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()

        webdriver_status = await page.evaluate("navigator.webdriver")
        print("from new_page: ", webdriver_status)

        different_context = await browser.new_context()
        page_from_different_context = await different_context.new_page()

        different_context_status = await page_from_different_context.evaluate("navigator.webdriver")
        print("from new_context: ", different_context_status)


asyncio.run(main())

Specifying config options and applying evasions manually to an entire context

import asyncio
from playwright.async_api import async_playwright
from playwright_stealth import Stealth, ALL_EVASIONS_DISABLED_KWARGS


async def advanced_example():
    # Custom configuration with specific languages
    custom_languages = ("fr-FR", "fr")
    stealth = Stealth(
        navigator_languages_override=custom_languages,
        init_scripts_only=True
    )

    async with async_playwright() as p:
        browser = await p.chromium.launch()
        context = await browser.new_context()
        await stealth.apply_stealth_async(context)

        # Test stealth on multiple pages
        page_1 = await context.new_page()
        page_2 = await context.new_page()

        # Verify language settings
        for i, page in enumerate([page_1, page_2], 1):
            is_mocked = await page.evaluate("navigator.languages") == custom_languages
            print(f"Stealth applied to page {i}: {is_mocked}")

    # Example of selective evasion usage
    no_evasions = Stealth(**ALL_EVASIONS_DISABLED_KWARGS)
    single_evasion = Stealth(**{**ALL_EVASIONS_DISABLED_KWARGS, "navigator_webdriver": True})

    print("Total evasions (none):", len(no_evasions.script_payload))
    print("Total evasions (single):", len(single_evasion.script_payload))


asyncio.run(advanced_example())

Todo

  • make this work with playwright.launch_persistent_context
    • the difficult because sometimes we sniff the UA if an override isn't provided, and this is difficult to do when launch_persistent_context is launched
  • sec-platform (we have navigator_platform)
  • docs

A set of Test results

playwright with stealth

playwright without stealth

playwright without stealth

playwright with stealth

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

playwright_stealth-2.0.3.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

playwright_stealth-2.0.3-py3-none-any.whl (34.4 kB view details)

Uploaded Python 3

File details

Details for the file playwright_stealth-2.0.3.tar.gz.

File metadata

  • Download URL: playwright_stealth-2.0.3.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.11.13 Linux/6.19.8-arch1-1

File hashes

Hashes for playwright_stealth-2.0.3.tar.gz
Algorithm Hash digest
SHA256 1d8e488fbdd8f190f1269ea8cf5d57d14df3a9f1af1001c41ee3588b2aac3133
MD5 8b9d2e1c133245c19bfbd38a9b1ab850
BLAKE2b-256 a4db6ade5d539c7d151b9defc78fafa8b65aa52352617d0e7699b47008bd801f

See more details on using hashes here.

File details

Details for the file playwright_stealth-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: playwright_stealth-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 34.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.11.13 Linux/6.19.8-arch1-1

File hashes

Hashes for playwright_stealth-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1887ade423ab7ff8ae16d363a30a38de0b5817e1e4a29d47b74bf3a0e3dbfcb4
MD5 40ad886f1ffe71f2d38a658d025f6202
BLAKE2b-256 b510607c409712c02a26c4cb794820514cb7fdaaeac15fb05bed917fb8a354b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page