A low-code platform blending no-code simplicity with full-code power 🚀
Get started free
Strategies for Bypassing Cloudflare Protection with Puppeteer
March 26, 2025
•
6
min read

Strategies for Bypassing Cloudflare Protection with Puppeteer

George Miloradovich
Researcher, Copywriter & Usecase Interviewer
Table of contents

Cloudflare's security measures make it tough for bots to access websites. But with Puppeteer, you can bypass these defenses if done ethically. Here's a quick guide to get started:

  • Core Techniques:
    • Use residential proxies and rotate them to avoid detection.
    • Mimic human behavior with random delays and mouse movements.
    • Handle JavaScript challenges and CAPTCHAs using plugins like puppeteer-extra-plugin-recaptcha.
    • Disguise automation by tweaking browser fingerprints and user agents.
  • Puppeteer Setup:
    • Install Puppeteer and plugins:
      npm install puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
      
    • Add stealth settings to avoid detection:
      const puppeteer = require('puppeteer-extra');
      const StealthPlugin = require('puppeteer-extra-plugin-stealth');
      puppeteer.use(StealthPlugin());
      
  • Legal Reminder: Always respect website terms of service, avoid overloading servers, and use automation responsibly.

These steps can improve your success rate against Cloudflare's protections. Dive into the article for detailed code snippets and advanced techniques.

Nodejs Puppeteer Tutorial #7 - Bypass Detection using ...

Puppeteer

Puppeteer Setup Guide

Follow these steps to configure Puppeteer with custom settings and plugins to navigate around Cloudflare protections.

Basic Puppeteer Installation Steps

First, make sure you have Node.js v18 or newer installed. Then, run the following command to install Puppeteer and its related plugins:

npm install puppeteer puppeteer-extra puppeteer-extra-plugin-stealth

Next, create a new JavaScript file and import the necessary modules:

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());

After this, you'll need to tweak the browser launch settings to avoid detection.

Required Puppeteer Settings

Set up your browser instance with configurations that lower the chance of being flagged:

const browser = await puppeteer.launch({
    headless: false,
    args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-infobars',
        '--window-position=0,0',
        '--ignore-certificate-errors',
        '--ignore-certificate-errors-spki-list'
    ]
});

If you want to use a proxy to mask your IP, include these additional settings:

const browser = await puppeteer.launch({
    args: [
        '--proxy-server=http://proxy-address:port'
    ]
});

await page.authenticate({
    username: 'proxy-username',
    password: 'proxy-password'
});

Useful Puppeteer Add-ons

Boost Puppeteer's functionality with these plugins:

Plugin Name Purpose Key Features
puppeteer-extra-plugin-stealth Anti-detection Modifies browser fingerprinting and mimics human behavior
puppeteer-extra-plugin-recaptcha CAPTCHA handling Automates CAPTCHA solving
puppeteer-extra-plugin-adblocker Resource management Blocks ads and trackers to reduce detection risks

To integrate these add-ons, use the following code:

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker');

puppeteer.use(StealthPlugin());
puppeteer.use(AdblockerPlugin({ blockTrackers: true }));

Finally, to make your actions look more natural, introduce random delays between them:

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
await delay(Math.random() * 1000 + 1000); // Random delay between 1-2 seconds
sbb-itb-23997f1

Methods to Handle Cloudflare Security

Cloudflare

Enhance your Puppeteer setup with these techniques to better navigate Cloudflare's defenses.

Browser Identity Management

Cloudflare's anti-bot system monitors browser fingerprints and automation signals. To disguise Puppeteer's activity, tweak browser identifiers and properties as shown below:

const browser = await puppeteer.launch({
    args: [
        '--window-size=1920,1080',
        '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
    ],
    ignoreDefaultArgs: ['--enable-automation']
});

await page.evaluateOnNewDocument(() => {
    delete navigator.webdriver;
    Object.defineProperty(navigator, 'plugins', {
        get: () => [1, 2, 3, 4, 5]
    });
});

This configuration removes automation indicators and mimics a standard browser fingerprint, helping bypass Cloudflare's checks.

JavaScript Challenge Solutions

Dealing with Cloudflare's JavaScript challenges requires simulating human-like behavior. For instance, you can wait for the challenge form to disappear and introduce random delays between actions:

// Wait until the Cloudflare challenge form is gone
await page.waitForFunction(() => {
    return document.querySelector('#challenge-form') === null;
}, { timeout: 30000 });

// Add random delays to simulate human interaction
const randomDelay = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1) + min);
};
await page.waitForTimeout(randomDelay(1000, 3000));

You can also create custom handlers to better mimic user behavior as needed. Next, you'll need a strategy for handling CAPTCHAs.

CAPTCHA Management Options

When faced with CAPTCHAs, using a CAPTCHA-solving plugin can simplify the process:

const RecaptchaPlugin = require('puppeteer-extra-plugin-recaptcha');
puppeteer.use(
    RecaptchaPlugin({
        provider: {
            id: '2captcha',
            token: 'your-api-key'
        }
    })
);

If the plugin fails to solve the CAPTCHA, you can switch to proxy rotation as a fallback:

try {
    await page.solveRecaptchas();
} catch (e) {
    // Rotate to a new proxy if CAPTCHA solving fails
    await page.setRequestInterception(true);
    await useNextProxy();
}

These methods help you navigate CAPTCHA challenges and maintain access, even when automation tools encounter roadblocks.

Reliability Tips and Guidelines

Implementing reliable techniques is key to ensuring success in automation.

Proxy Setup and Usage

Using well-managed proxies can help reduce detection risks significantly. For example, premium residential proxies offer better bypass capabilities. Here's a setup example:

const browser = await puppeteer.launch({
    args: [
        `--proxy-server=${proxyAddress}`,
        '--no-sandbox',
        '--disable-setuid-sandbox'
    ]
});

// Handle proxy authentication
await page.setRequestInterception(true);
page.on('request', request => {
    request.continue({
        headers: {
            ...request.headers(),
            'Proxy-Authorization': `Basic ${Buffer.from(
                `${proxyUsername}:${proxyPassword}`
            ).toString('base64')}`
        }
    });
});

Rotating proxies regularly also helps maintain uninterrupted access:

const proxyList = [
    'proxy1.example.com:8080',
    'proxy2.example.com:8080',
    'proxy3.example.com:8080'
];

function getNextProxy() {
    const proxy = proxyList.shift();
    proxyList.push(proxy);
    return proxy;
}

Request Timing Control

Simulating human-like behavior can minimize detection risks. Here's how you can manage request timing effectively:

const simulateHumanBehavior = async (page) => {
    // Add random mouse movements
    await page.mouse.move(100, 200);
    await page.mouse.move(150, 250, { steps: 10 });

    // Randomize typing speed
    await page.keyboard.type('Hello World', { 
        delay: Math.floor(Math.random() * (150 - 50) + 50) 
    });

    // Add pauses
    await page.waitForTimeout(
        Math.floor(Math.random() * (3000 - 1000) + 1000)
    );
};

These techniques, combined with proper session handling, make automation efforts more reliable.

Managing cookies effectively is crucial for maintaining session continuity. Here's how you can store and restore sessions:

// Store successful session cookies
const storeCookies = async (page) => {
    const cookies = await page.cookies();
    await fs.writeFile(
        'cookies.json',
        JSON.stringify(cookies, null, 2)
    );
};

// Restore previous session
const loadCookies = async (page) => {
    try {
        const cookiesString = await fs.readFile('cookies.json');
        const cookies = JSON.parse(cookiesString);
        await page.setCookie(...cookies);
    } catch (error) {
        console.log('No stored cookies found');
    }
};

If cookies fail validation, you can refresh them automatically:

const validateCookies = async (page) => {
    const response = await page.goto(targetUrl);
    if (response.status() === 403) {
        await clearCookies(page);
        await initializeNewSession(page);
    }
};

Conclusion

Let's wrap up by highlighting the main techniques and steps we've covered.

Key Techniques Recap

Getting past Cloudflare protection requires a mix of methods working together. The most important strategies include:

  • Using residential proxies with proper rotation
  • Managing browser fingerprints and user agents
  • Handling cookies and sessions effectively
  • Mimicking human behavior with random delays
  • Setting accurate request headers
  • Managing authentication correctly

According to industry data, combining these techniques can lead to success rates as high as 98.7% when conditions are optimized.

By focusing on these core methods, you can build a reliable and compliant automation process.

Steps for Implementation

Here are some final tips to refine your Puppeteer setup and Cloudflare bypass efforts:

  • Begin with basic Puppeteer configurations
  • Rotate proxies thoughtfully
  • Set up strong error-handling mechanisms
  • Check the target site's terms of service before starting
  • Keep request rates moderate to avoid triggering defenses
  • Document your automation workflows

Puppeteer requires careful setup and regular tweaking to stay effective against evolving Cloudflare defenses. As security measures grow more advanced, success will depend on ongoing updates to your strategy and strict adherence to ethical practices. While initial configurations might work well, maintaining long-term performance means staying flexible and following clear guidelines.

Related posts

Related Blogs

Use case

Backed by