How to Bypass CAPTCHA in 2024#

What you're going to learn?#

you'll learn how to bypass CAPTCHA with Puppeteer in Nodejs using different methods:

Can Puppeteer Bypass CAPTCHA?#

The answer is yes, there are two main ways to bypass CAPTCHAs: using a CAPTCHA solver(fast and cheaper), or by implementing advanced techniques to prevent them

Method #1: Bypass CAPTCHA with Puppeteer and NextCaptcha#

We'll work with the solver service called NextCaptcha to handle CAPTCHA in Puppeteer using a official demo. So, let's installing some dependencies and import the modules
  npm install -S puppeteer nextcaptcha-ts
  import NextCaptcha from 'nextcaptcha-ts';
  import puppeteer from 'puppeteer';
Then, open a Chrome instance and navigate to the demo page.
  const url = "https://www.google.com/recaptcha/api2/demo"

  const browser = await puppeteer.launch({});
  const page = await browser.newPage();
  await page.goto(url, {
    waitUntil: 'networkidle0',
  });
The next step is to locate the CAPTCHA websiteKey and websiteURL to the  NextCaptcha client, which returns the text solution. Save it in the data.solution.gRecaptchaResponse variable.
  const data = await client.recaptchaV2({websiteKey, websiteURL: url});
  console.log('gRecaptchaResponse: ', data.solution.gRecaptchaResponse);
  console.log('submitting form .. ');
  await Promise.all([
      page.click('#recaptcha-demo-submit'),
      page.waitForNavigation({ waitUntil: "networkidle0" })
  ]);

  console.log('making a screenshot ...');
  await page.screenshot({ path: 'screenshot.png' });

  console.log('closing browser .. ');
  await browser.close();

Method #2: Implement Puppeteer Stealth#

The use of Puppeteer is easy to be identified since its base version sends some clear bot signals, such as its User-Agent name, which might easily prompt a CAPTCHA. And to prove this, let's try to access sannysoft.com, a check site. For that, create a headless Chrome instance, pass the target URL to the goto() function to wait for the page to load, and take a screenshot.
  import NextCaptcha from 'nextcaptcha-ts';
  import puppeteer from 'puppeteer';

  const url = "https://bot.sannysoft.com"
  const browser = await puppeteer.launch({
      headless: true
  });
  const page = await browser.newPage();
  await page.goto(url, {
      waitUntil: 'networkidle0',
  });
  console.log('making a screenshot ...');
  await page.screenshot({ path: 'screenshot.png' });
  await sleep(30000)
  console.log('closing browser .. ');
  await browser.close();


  function sleep(time = 1000) {
      return new Promise((resolve) => {
          setTimeout(() => {
              resolve(true)
          }, time)
      })
  }
It will show you are WebDrive present(failed) Access denied!  sannysoft detected non-human traffic and blocked our bot. This is where the puppeteer-extra-plugin-stealth, A plugin for puppeteer-extra and playwright-extra to prevent detection, comes to the rescue. It will make your traffic look more manual and prevent getting blocked, i.e. with CAPTCHAs. To start with, install the Stealth package
  npm install puppeteer-extra puppeteer-extra-plugin-stealth
  import puppeteer from 'puppeteer-extra';
  import puppeteer-extra-plugin-stealth from 'puppeteer-extra-plugin-stealth';

  const StealthPlugin = require('puppeteer-extra-plugin-stealth')
  puppeteer.use(StealthPlugin())
then use the puppeteer as the normal
  const url = "https://bot.sannysoft.com"
  const browser = await puppeteer.launch({
      headless: true
  });
  const page = await browser.newPage();
  await page.goto(url, {
      waitUntil: 'networkidle0',
  });
  console.log('making a screenshot ...');
  await page.screenshot({ path: 'screenshot.png' });
  await sleep(30000)
  console.log('closing browser .. ');
  await browser.close();


  function sleep(time = 1000) {
      return new Promise((resolve) => {
          setTimeout(() => {
              resolve(true)
          }, time)
      })
  }
Let's look at the output: check-success Success! This helps bypass CAPTCHA with Puppeteer .

Conclusion#

For successful data retrieval, you need a powerful tool to rely completely on in order to handle CAPTCHA. NextCaptcha provides an easy-to-setup API that enables you to overcome all anti-bot challenges, and you can try it for free today.