Automatically solve any captcha as part of your Playwright or Puppeteer script

Zach Goldie
Zach Goldie
/
March 28, 2024

If you run browser automations such as web scraping, you’ve probably come across websites that stop your scripts with a captcha

Let Browserless handle your captcha solving

There are some websites or UI steps that will force you to solve a captcha, such as when logging to some apps. For these automations, no amount of stealth settings will convince the captchas to let you through (more on this below).

That’s why we now offer captcha solving at Browserless.

All you need to do is add the following snippet to your Puppeteer or Playwright script:


 const cdp = await page.createCDPSession();
 const { solved, error } = await cdp.send('Browserless.solveCaptcha');
 console.log({
    solved,
    error,
 });

That’s it! We’ll handle the hard work from there.

How it works

To trigger the API, we’ve added foundCaptcha. This is a new ability to check if there is a captcha on a page, so the script can decide whether or not to proceed with solving.

If a captcha is detected, solveCaptcha runs a custom Chrome extension in the page that can automatically detect captchas. Putting the two together, the code becomes


import puppeteer from 'puppeteer-core';

// Recaptcha
(async () => {
  const browser = await puppeteer.connect({
    browserWSEndpoint:
      'wss://production-sfo.browserless.io/chromium/stealth?token=YOUR-API-TOKEN&timeout=300000',
  });

  const page = await browser.newPage();
  const cdp = await page.createCDPSession();

  await page.goto('https://www.google.com/recaptcha/api2/demo', {
    waitUntil: 'networkidle0',
  });

  await new Promise((resolve) =>
    cdp.on('Browserless.captchaFound', () => {
      console.log('Found a captcha!');
      return resolve();
    }),
  );

  const { solved, error } = await cdp.send('Browserless.solveCaptcha');
  console.log({
    solved,
    error,
  });

  // Continue...
  await page.click('#recaptcha-demo-submit');
})().catch((e) => {
  console.error(e);
  process.exit(1);
});

That extension collects the captcha’s token and sends it to a secondary service to be solved. Once that’s happened, the captcha will let you through. Here’s how it looks in action:

Your scripts won’t need to do any extra work, all that’s needed is to wait until the Captcha is solved with our custom Browserless CDP API.

A note on captcha solving vs bypassing

Broadly speaking, there are two types of captchas.

  • Active captchas will show a captcha to every user attempting to complete a certain step
  • Passive captchas will check for signs that the traffic is a bot, and either show a visible captcha or block it if it thinks the visit is suspicious

Lots of websites will use passive captchas, either invisibly or via an “are you human” checkbox.

For sites with a passive captcha, we would recommend starting with our trio of stealth settings. These hide the most obvious signs that the visit is coming from an automated browser.

If a passive captcha is still blocking the traffic, then the next step should be to try our /unblock API. It changes fundamental settings at the CDP level to mask even subtle fingerprints that the captchas look for.

It is only when those two fail or if there is an active captcha, that we would recommend using the captcha solver or a hybrid automation.

Want to add captcha solving to your scripts?

Captcha solving is now available for users on our Scale or Enterprise plan. It uses 10 units per solve, counted against your monthly allowance.
Try it yourself on a 7 day trial, or contact our team about using it in an enterprise account.

Share this article

Ready to try the benefits of Browserless?

Sign Up