How to Use the 2Captcha Solver Extension in Puppeteer for Bypassing Captchas

CAPTCHAFORUM

Administrator
https://2captcha.com/blog/how-to-use-2captcha-solver-extension-in-puppeteer

Puppeteer is a powerful tool for automating web applications for testing and parsing. However, it can be challenging to bypass captcha checks in Puppeteer. A special recognition automation service is required to solve captchas automatically. 2Captcha is a captcha-solving service that can be integrated with Puppeteer to bypass captchas on any site.

Here is a step-by-step guide on how to use the 2Captcha solver extension in Puppeteer for bypassing captchas:

1. Download the 2Captcha solver extension and unzip it to the folder `./2captcha-solver` in the root of the project.
2. Install the following libraries: `puppeteer`, `puppeteer-extra`, `puppeteer-extra-plugin-stealth`, and `2captcha-solver`.
3. Write a JavaScript file where you write captcha-solving code.
4. Write Puppeteer code to visit the target page and solve the captcha using 2Captcha.

Here is an example of how to use Puppeteer and 2Captcha to solve reCAPTCHA V2:

Code:
javascript
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const { executablePath } = require('puppeteer');
const { TwoCaptcha } = require('2captcha');


(async () => {
  const solver = new TwoCaptcha('YOUR_API_KEY');
  const siteKey = 'SITE_KEY';


  puppeteer.use(StealthPlugin());
  const browser = await puppeteer.launch({
    headless: false,
    args: [
      '--disable-extensions-except=./2captcha-solver',
      '--load-extension=./2captcha-solver',
    ],
    executablePath: executablePath(),
  });


  const page = await browser.newPage();
  await page.goto('https://www.google.com/recaptcha/api2/demo');


  const frame = await page.frames().find(f => f.name() === 'c-4vchutkjuek');
  const recaptcha = await frame.$('#recaptcha-anchor');


  await recaptcha.click();


  const result = await solver.recaptcha(siteKey, page.url());
  await frame.$eval('#g-recaptcha-response', (el, value) => el.innerHTML = value, result.code);


  await page.click('#recaptcha-demo-submit');
})();

Congratulations, the captcha has been successfully passed! The full source code of the example is available on GitHub.

Useful Information

- The extension has many settings, for example, automatic solution of the specified type of captcha or support for proxy, all these settings are available in the file `./common/config.js`, in the folder with the unpacked extension.
- To automatically send reCAPTCHA V2, in the file `./common/config.js` change the value of the `autoSolveRecaptchaV2` field to `true`.
- You can use sandbox mode for testing. In sandbox mode, sent captchas will be sent to you. Learn more about sandbox you can read here.
- Puppeteer running in headless mode does not support loading extension, to solve this problem you can use xvfb.

In conclusion, Puppeteer and 2Captcha can be used together to bypass captchas on any site. The process is straightforward, and the pricing is reasonable. The service is regularly used by thousands of users, and there are several tutorials available online that demonstrate how to use Puppeteer and 2Captcha to solve captchas.