Patchright is a patched version of Playwright that bypasses modern bot detection systems by fixing critical CDP (Chrome DevTools Protocol) leaks and browser fingerprinting issues.
Instead of leaving telltale signs like the Runtime.enable command that flags the session as automated, Patchright executes JavaScript in isolated contexts, so anti-bot systems can't detect the automation through JavaScript property checks.
If Cloudflare, DataDome, or another anti-bot system is blocking your scraper, vanilla Playwright is usually the reason: those systems spot it instantly through CDP leaks. Switching from Playwright to Patchright takes one line of code, and your bot starts passing detection tests that would normally block it in seconds.
One caveat before you start. Patchright closes the JavaScript-property and CDP leak channels, but it leaves your TLS fingerprint and your IP reputation untouched. To get past Cloudflare Turnstile and DataDome at scale, you'll still need residential proxies and JA3/JA4 fingerprint alignment on top of it.
In this post, I'll show you how to set up and use Patchright for undetectable web scraping, including a trick for handling closed Shadow DOM elements that normal scrapers can't touch.
Why Patchright avoids detection when Playwright doesn't
Patchright is a patched build of Playwright that removes the automation signal anti-bot systems check for first. Stock Playwright sends the Runtime.enable CDP command when it starts working with a page. Cloudflare and DataDome look for that command specifically, and it is enough on its own to mark the browser as automated. Changing your user agent or adding stealth plugins on top of regular Playwright leaves that command in place, so the browser gets flagged either way.
How the patch works
Patchright changes Playwright at the source level so it never calls Runtime.enable. It runs your JavaScript inside isolated ExecutionContexts instead. The page's own scripts can't see those contexts, so a detection script running on the site has no CDP signal to find. The fix sits inside the driver, not in a layer you add afterwards, which is why it holds up against checks that defeat surface-level stealth tweaks.
Because the public API stays the same, Patchright works as a drop-in replacement for Playwright. In most scripts you swap the import and leave the rest of your code alone. It ships as a Python package on PyPI and as a Node.js package, both under the Apache-2.0 license.
Detection tests Patchright passes
With proper configuration, Patchright passes these detection tests:
- Brotector (only when combined with CDP-Patches)
- Cloudflare
- DataDome
- CreepJS, with a 0% headless score
- Fingerprint.com
- BrowserScan
The phrase "with proper configuration" matters here. These results describe the browser fingerprint. Patchright does nothing about your IP address, so a scraper on a flagged datacenter IP, or one sending requests too fast, can still get blocked even when every fingerprint check passes. Treat Patchright as the fix for browser-level detection, and handle proxies and request pacing on their own.
Patchwright vs Patchright: same tool
"Patchwright" is a common misspelling of Patchright. It comes from mixing up the name with Playwright, the library it patches. No separate project called Patchwright exists. If you searched for patchwright, the library covered in this guide is the one you want. Install it under its real name, patchright, because a search for the misspelled package won't return it.
Step 1: Install Patchright (The Right Way)
Patchright only works with Chromium-based browsers. Forget about Firefox or WebKit support; they're not happening.
For Python Users:
# Install Patchright from PyPI
pip install patchright
# Install Chrome (not Chromium!) for better stealth
patchright install chrome
For Node.js Users:
# Install via npm
npm install patchright
# Install Chrome browser
npx patchright install chrome
Pro Tip: Always use Google Chrome instead of Chromium. Real users don't browse with Chromium, and anti-bot systems know this. Using channel="chrome" makes you blend in with the crowd.
Step 2: Configure for Maximum Stealth
Most people mess up here: they think installing Patchright is enough. Wrong. You need the right configuration to be truly undetectable.
Python Configuration:
from patchright.sync_api import sync_playwright
with sync_playwright() as p:
# Launch with stealth configuration
browser = p.chromium.launch_persistent_context(
user_data_dir="/tmp/patchright_profile", # Use a real profile
channel="chrome", # Use real Chrome, not Chromium
headless=False, # Never use headless for critical scraping
no_viewport=True, # Disable viewport to use native resolution
# CRITICAL: Do NOT add custom headers or user_agent here!
)
page = browser.new_page()
page.goto("https://example.com")
# Your scraping logic here
browser.close()
Node.js Configuration:
const { chromium } = require('patchright');
(async () => {
const browser = await chromium.launchPersistentContext('/tmp/patchright_profile', {
channel: 'chrome',
headless: false,
viewport: null,
// Don't add custom browser headers or userAgent
});
const page = await browser.newPage();
await page.goto('https://example.com');
// Your scraping logic here
await browser.close();
})();
The Hidden Tweaks Patchright Makes:
Behind the scenes, Patchright automatically applies these command-line flags:
--disable-blink-features=AutomationControlled(removes navigator.webdriver)- Removes
--enable-automationflag - Removes
--disable-popup-blockingto prevent crashes - Removes
--disable-component-updateto avoid detection - Enables extensions and default apps (like a real browser)
Warning: The console API is completely disabled in Patchright. If you need logging, use a JavaScript logger library instead of console.log().
Step 3: Execute JavaScript Without Getting Caught
This is Patchright's key difference. Instead of using the detectable Runtime.enable command, it executes JavaScript in isolated contexts.
Using Isolated Context (Default & Recommended):
# Python example with isolated context
result = page.evaluate("""
() => {
// This runs in an isolated context, undetectable by anti-bot systems
return document.querySelector('.price').innerText;
}
""", isolated_context=True) # Default is True
Switching to Main Context (When Necessary):
Sometimes you need access to the main window context (rare, but it happens):
# Access main context when absolutely necessary
result = page.evaluate("""
() => {
// This runs in main context - use sparingly!
return window.customGlobalVariable;
}
""", isolated_context=False)
The Shadow DOM Superpower:
Patchright can interact with closed Shadow DOM elements, which vanilla Playwright can't do:
# This just works in Patchright, even with closed shadow roots!
element = page.locator('custom-element >>> .shadow-content')
element.click()
# XPath works too
shadow_element = page.locator('//custom-element//div[@class="shadow-content"]')
Step 4: Test Your Setup Against Detection Systems
Verify it. Here's how to test against real detection systems:
Quick Detection Test:
import time
from patchright.sync_api import sync_playwright
def test_detection():
with sync_playwright() as p:
browser = p.chromium.launch_persistent_context(
user_data_dir="/tmp/test_profile",
channel="chrome",
headless=False,
no_viewport=True
)
page = browser.new_page()
# Test 1: CreepJS (should show 0% headless)
page.goto("https://abrahamjuliot.github.io/creepjs/")
time.sleep(5)
# Test 2: BrowserScan CDP detection
page.goto("https://www.browserscan.net/")
time.sleep(5)
# Test 3: Rebrowser bot detector
page.goto("https://bot.rebrowser.net/")
time.sleep(5)
input("Check the results and press Enter to close...")
browser.close()
test_detection()
What to Look For:
- CreepJS: Headless score should be 0%
- BrowserScan: No CDP detection warning
- Rebrowser: Should pass all tests except potentially timing-based ones
Advanced Bypass Technique:
For sites with extreme detection (looking at you, Cloudflare), combine Patchright with request interception:
async def stealth_request(page, url):
# Intercept and modify requests for extra stealth
await page.route('**/*', lambda route: route.continue_({
'headers': {
**route.request.headers,
'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"'
}
}))
await page.goto(url)
Common Pitfalls to Avoid
- Using Headless Mode: Just don't. Modern detection flags headless browsers through missing GPU info, viewport mismatches, and timing analysis.
- Custom User Agents: Adding custom user agents in the browser launch options is a dead giveaway. Let Patchright handle it.
Ignoring Mouse Movement: Real users move their mouse. Simulate it:
await page.mouse.move(100, 100)
await page.mouse.move(200, 300, steps=10)
Fast Navigation: Don't immediately start clicking and scraping. Add realistic delays:
await page.wait_for_timeout(random.randint(2000, 5000))
Final Thoughts
Patchright solves the biggest problem with web scraping today: the CDP detection that gets most bots instantly blocked. By executing JavaScript in isolated contexts and patching critical browser leaks, it passes the detection tests listed above.
Remember: Patchright bypasses browser-level detection, but you still need to act human. Combine it with proper delays, mouse movements, and realistic browsing patterns for the best results.