Thursday, May 1, 2025

How To Make Playwright Undetectable

Making Playwright undetectable can be a complex process because many websites employ techniques to detect automation tools like Playwright. These techniques typically involve detecting browser behavior, JavaScript properties, and specific browser fingerprints that automation tools exhibit. However, there are various strategies you can implement to reduce the chances of detection:

How To Make Playwright Undetectable


How to Make Playwright Undetectable

To make Playwright undetectable, you must customize browser settings, spoof user agent strings, disable automation flags, and use realistic interaction patterns. By doing this, you can reduce the likelihood of websites detecting your automated scripts.

We can see an example of implementing this in a Playwright script:

const { chromium } = require('playwright');

(async () => {
    const browser = await chromium.launch({
        headless: false,
        args: ['--disable-blink-features=AutomationControlled']
    });
    const context = await browser.newContext({
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        viewport: { width: 1280, height: 720 },
        deviceScaleFactor: 1,
    });
    const page = await context.newPage();
    await page.goto('https://cnn.com');

    await browser.close();
})();
Disable Automation Flags: We can use the --disable-blink-features=AutomationControlled argument to remove the automation flag, which can help prevent detection.

Set Realistic Viewport and Device Characteristics: Configure the browser context to match typical user settings. We will set the viewport to a typical display so we don't raise any flags.

Modify User Agent String: We can use different user agent strings to appear as a more commonly used browser.


How To Make Playwright Undetectable To Anti-Bots

Different techniques can be used to make your Playwright undetectable to websites’ anti-bot mechanisms.

Here are some of the most commonly used methods.

1. Setting User-Agent Strings

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requestingbrowser.

We can modify the user-agent string to mimic popular browsers, ensuring it matches the browser version and operating system. We can set up the browser user agent string like this in Playwright

const context = await browser.newContext({
    userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
});


2. Enabling WebGL and Hardware Acceleration

We can ensure WebGL and hardware acceleration are enabled to replicate the typical capabilities of a human-operated web browser by specifying specific arguments when launching our Chromium browser.

const browser = await chromium.launch({
    args: [
        '--enable-webgl',
        '--use-gl=swiftshader',
        '--enable-accelerated-2d-canvas'
    ]
});

3. Using Rotating Residential Proxies

You can also use rotating residential proxies to change your IP addresses regularly to avoid multiple requests from the same IP, which can raise red flags.

const browser = await chromium.launch({
    proxy: {
        server: 'http://myproxy.com:3128',
        username: 'usr',
        password: 'pwd'
    }
});

4. Delay and Humanize Interactions

Websites can detect automation by tracking patterns like too fast or predictable actions. Introduce randomized delays between actions to simulate human-like behavior:

const randomDelay = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
await page.click('button');
await page.waitForTimeout(randomDelay(1000, 2000)); // Random delay

5. Using Playwright with Real Browsers

We can run Playwright with full browser environments rather than headless modes to reduce the likelihood of detection. You can launch your Chromium or Firefox browser by following the script below:

const browser = await chromium.launch({ headless: false });

const browser = await firefox.launch({ headless: false });


A stealthy Playwright setup offers significant advantages. It provides improved access to web content, bypassing measures that limit or block automated access. This is particularly beneficial for web scraping or testing tasks, allowing for efficient data collection and smooth operation.


This ensures the uninterrupted execution of automation tasks, making Playwright an invaluable tool in web automation.


No comments:

Post a Comment