Automatically Solving and Bypassing reCAPTCHA in Puppeteer
Tags: puppeteer, javascript, webdev, programming
Published: 2023-11-17 07:33:56


![Selenium](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5g4urjyhi3uz1octvcuq.jpg)

Puppeteer, a Node.js library, offers a powerful platform for browser automation. Its capability extends to operating in headless mode, allowing users to automate tasks while masquerading as a regular user. This article delves into using the puppeteer-extra-plugin-stealth plugin to bypass reCAPTCHA in Puppeteer, making your automation tasks smoother and more efficient.

> Note: I take this Guide in this manual - [How to solve reCAPTCHA in Puppeteer using extension](https://2captcha.com/p/puppeteer-captcha-solver)
Prerequisites:
To get started, you'll need:

The 2captcha.com service for CAPTCHA solving.
Puppeteer and puppeteer-extra.
puppeteer-extra-plugin-stealth for hiding automation traces.
Installation:
Begin by installing the required packages. Open your console and enter:

b

```
npm i puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
```
Setting Up the Extension:
Download the extension from the provided link and extract it to the `./2captcha-solver` directory in your project root. This extension comes with various settings, like automatic CAPTCHA solving for specific types, proxy support, etc. Modify the settings in the `./common/config.js` file. For solving reCAPTCHA V2, set `autoSolveRecaptchaV2` to true and insert your `API key` from 2captcha.com in quotes to avoid errors.

Browser Automation:
Initiate Puppeteer with the stealth plugin and the extension path:

```
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const { executablePath } = require('puppeteer');

(async () => {
  const pathToExtension = require('path').join(__dirname, '2captcha-solver');
  puppeteer.use(StealthPlugin());
  const browser = await puppeteer.launch({
    headless: false,
    args: [
      `--disable-extensions-except=${pathToExtension}`,
      `--load-extension=${pathToExtension}`,
    ],
    executablePath: executablePath(),
  });
  const [page] = await browser.pages();
})();
```
Solving the CAPTCHA:
Navigate to the desired page using `page.goto()` and wait for the CAPTCHA solver button to appear using its CSS selector. Click the button to send the CAPTCHA for solving:

```
await page.goto('https://2captcha.com/demo/recaptcha-v2');
await page.waitForSelector('.captcha-solver');
await page.click('.captcha-solver');
```
Monitoring CAPTCHA Resolution:
Track the CAPTCHA solving process by monitoring the `'data-state'` attribute of the solver button. Wait until the attribute changes to "solved", indicating successful CAPTCHA resolution:

```
await page.waitForSelector(`.captcha-solver[data-state="solved"]`, { timeout: 180000 });
```
Final Steps and Verification:
After solving the CAPTCHA, perform the necessary page actions. In this example, click the "Check" button to verify the CAPTCHA solution:

```
await page.click("button[type='submit']");
```

This guide provides a straightforward method to automate CAPTCHA solving in Puppeteer, leveraging the puppeteer-extra-plugin-stealth and 2captcha.com service. 

Ready-to-Use File Download:
For convenience, I’ve provided a ready-to-use file that includes all the necessary configurations. This file can be downloaded using the link below. Remember, after downloading and unzipping this file, you’ll need to add the solver folder (discussed earlier) into it. This step ensures that all components are in place and the setup is ready for immediate use.

[https://github.com/2captcha/2captcha-solver-in-puppeteer
](https://github.com/2captcha/2captcha-solver-in-puppeteer
)
Conclusion: This guide demonstrates how to effectively automate reCAPTCHA solving in Puppeteer, providing a significant advantage in web scraping and automated testing scenarios. It’s important to use these techniques responsibly and ethically.