How to Use the 2Captcha solver with Puppeteer

This article will show you how to use 2captcha solver with the Puppeteer library. Puppeteer, a Node library, enables you to control a browser and perform various actions, and its ease of use and headless mode capabilities make it an excellent tool for automation tasks. The automation process can be disguised by using Puppeteer plugins, making it useful for developing parsers by making the browser appear more human-like and avoiding detection.

Introduction to 2Captcha Solver

2Captcha Solver is an extension for Chrome that allows users to quickly solve captchas. Captchas, which stand for Completely Automated Public Turing Test to Tell Computers and Humans Apart, are used to prevent automated bots from accessing a website. Image recognition, math problems, and distorted text are examples of these. 2Captcha Solver employs real human workers to solve captchas quickly and accurately, making it an invaluable tool for internet task automation. It also includes an API that can be used in conjunction with other programs or scripts, such as the Puppeteer library, to automate the process of solving captchas.

How to use 2Captcha solver with Puppeteer Library

We will use the Puppeteer library, as well as puppeteer-extra and puppeteer-extra-plugin-stealth, in this article. The puppeteer-extra package is a convenient wrapper for Puppeteer, and the puppeteer-extra-plugin-stealth plugin is designed to hide any signs of automation when using Puppeteer. Together, these tools will enable efficient and covert browser automation.

Step 1: Installing Packages

It is simple to install the required packages for using Puppeteer, puppeteer-extra, and puppeteer-extra-plugin-stealth. The first step is to ensure that your computer has Node.js and npm (Node Package Manager) installed. If you don’t already have these, you can get them from the official Node.js website.

Once Node.js and npm are installed, run the following command in your terminal or command prompt to install the packages:

npm i puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
JavaScript

This command will install Puppeteer, puppeteer-extra, and puppeteer-extra-plugin-stealth and their dependencies in your project.

Step 2: Configuration

To get started, download the extension archive and extract it to a folder called “2captcha-solver” at the root of your project.

The extension has a number of options, including automatic captcha solving and proxy support. These options can be found and changed in the “config.js” file, which is located in the “common” folder. Change the “autoSolveRecaptchaV2” field in the config.js file to “true” to enable automatic solving for reCAPTCHA V2.

The extension must then be configured:

In the “config.js” file, enter your API key. The value for the “apiKey” field must be your key. Your API key is available on the 2Captcha website.

Remove the following lines from the “manifest.json” file to prevent the extension settings page from opening after installation:

"options_ui": {
    "page": "options/options.html",
    "open_in_tab": true
},
JavaScript

After you finish these steps, the extension should be properly configured and ready to use in your Puppeteer script.

Step 3: Let the magic happen

The following packages are required to launch and initialize the extension in Puppeteer:

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

Then, use the following code to launch the browser and load the extension:

(async () => {
  const extensionPath = require('path').join(__dirname, '2captcha-solver');
  puppeteer.use(stealth());
  const browser = await puppeteer.launch({
    headless: false,
    args: [
      `--disable-extensions-except=${extensionPath}`,
      `--load-extension=${extensionPath}`,
    ],
    executablePath: executablePath()
  });
  const [firstPage] = await browser.pages()
})();
JavaScript

Opening a page and sending a captcha:

You can use the following code snippet to open a page and send a captcha. It goes to https://2captcha.com/demo/recaptcha-v2 and sends a captcha in search of a solution. The captcha is sent manually in this example by waiting for the extension button with the CSS selector ‘.captcha-solver’ to become available and then clicking on it.

(async () => {
// Opening a page with a captcha
await firstPage.goto('https://2captcha.com/demo/recaptcha-v2')

// Waiting for the element with the class "captcha-solver"
await firstPage.waitForSelector('.captcha-solver')
// Select the element with the specified selector by clicking on it.
await firstPage.click('.captcha-solver')
})();
JavaScript

Checking the status of the captcha:

After sending the captcha, you can check the status of the solution by looking at the data-state attribute on the extension button. The attribute will have different values depending on the captcha’s status.

Let’s see all the possible states of the data-sate attribute.

StateDescription
readyThe captcha has been solved by the extension. You must click on the button to send a captcha.
solvingThe captcha is being worked on right now.
solvedThe captcha has been solved successfully.
errorAn error occurred while receiving a response, or the captcha could not be solved successfully.

Note

You should wait until the attribute’s value changes to “solved” before taking any further action.

You can use this code snippet to check the status of the captcha.

await firstPage.waitForSelector('.captcha-solver[data-state="solved"]')
JavaScript

It waits until the ‘.captcha-solver’ selector with a data-state attribute of “solved” becomes available.

Step 4: More Actions

After you’ve successfully solved the captcha, you can move on to the next step on the page. In this example, we will click the “Check” button to ensure that the captcha solution received is correct. If the solution is correct, the message “Captcha is successfully passed!” will appear.

// Click the "Check" button to verify that the captcha was successfully solved.
await firstPage.click("button[type='submit']")
JavaScript

That’s all there is to it! You successfully completed the captcha by combining the 2Captcha Solver extension with Puppeteer. The example’s complete source code is available on the 2Captcha website.


Frequently Asked Questions

Q: How do I get an API key for the 2Captcha Solver extension?

A: You can obtain an API key by creating an account on the 2Captcha website. You can find your API key in the account dashboard once you’ve created an account.

Q: Can I use the 2Captcha Solver extension with tools other than Puppeteer?

A: Yes, you can use the 2Captcha Solver extension with other automation tools that support browser extensions, such as Selenium.

Q: What kinds of captchas can the 2Captcha Solver extension solve?

A: The 2Captcha Solver extension supports various captchas including reCAPTCHA V2, reCAPTCHA V3, and custom captchas.

Q: Is it possible to use a proxy with the 2Captcha Solver extension?

A: Yes, the extension includes built-in proxy support. The proxy settings can be changed in the “config.js” file.

Q: What’s the distinction between a puppeteer and a puppeteer-extra?

A: Puppeteer is a Node.js library for controlling a web browser, whereas puppeteer-extra is a lightweight wrapper around Puppeteer that adds functionality.

Leave a Comment

Developer Wings

To become a good programmer, you must be updated with the latest technologies and methods. Subscribe to our newsletter and get updated on the latest trends, technologies, and methods.