Menu

Bypassing Recaptcha V2 with NodeJS and Puppeteer


What you're going to learn

  • Set up NodeJS and Webstorm IDE in freshly installed Ubuntu OS.
  • Install and import anticaptchaofficial and puppeteer npm modules.
  • You will learn how to submit this form with Recaptcha with NodeJS script.
  • Learn how to write a puppeteer script which opens Chromium in background mode and navigates to a page.
  • Learn how to take a screenshot of a page and save it on a disk.
  • Learn how to use CSS selectors to fill text fields in a form.
  • Learn how to solve Recaptcha v2, fill hidden textarea #g-recaptcha-response, and submit the form.
  • Notes on extending your knowledge of Javascript ES6 and NodeJS.
Source code

Set up NodeJS and Webstorm IDE in freshly installed Ubuntu OS.

  • Ubuntu OS: Installing Ubuntu OS is not a requirement, but a recommendation. Running NodeJS from Windows is also possible, but our code may not be compatible.
  • Node JS: Download NodeJS for your operating system here
  • Code editor: Recommended VS Code or Atom. Both are free IDE.

Install curl and nodejs in Ubuntu:

apt update
apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
apt install -y nodejs

Install and import anticaptchaofficial and puppeteer npm modules.

Install anti-captcha and Puppeteer npm modules:

npm install @antiadmin/anticaptchaofficial puppeteer

Create a new file index.js and add the following. Replace "YOUR_API_KEY" with your key from API settings :

const ac = require("@antiadmin/anticaptchaofficial");
      const pup = require("puppeteer");
      ac.setAPIKey('YOUR_API_KEY');

Learn how to write a puppeteer script which opens Chromium in background mode and navigates to a page.

Add the following code to first solve captcha, then fill the form with login and password, paste g-response token to the textarea with id="g-recaptcha-response" and submit the form:

const login = 'mylogin';
const password = 'my strong password';

(async () => {

    console.log('solving recaptcha ...');
    let token = await ac.solveRecaptchaV2Proxyless('https://anti-captcha.com/demo?page=recaptcha_v2_textarea', '6LfydQgUAAAAAMuh1gRreQdKjAop7eGmi6TrNIzp');
    if (!token) {
        console.log('something went wrong');
        return;
    }

    console.log('opening browser ..');
    const browser = await pup.launch();

    console.log('creating new tab ..');
    const tab = await browser.newPage();

    console.log('changing window size .. ');
    await tab.setViewport({ width: 1360, height: 1000 });

    console.log('opening target page ..');
    await tab.goto('https://anti-captcha.com/demo?page=recaptcha_v2_textarea', { waitUntil: "networkidle0" });

    console.log('filling login input ..');
    await tab.$eval('#contentbox > form > div > div:nth-child(1) > span > input', (element, login) => {
        element.value = login;
    }, login);

    console.log('filling password input');
    await tab.$eval('#contentbox > form > div > div:nth-child(2) > span > input', (element, password) => {
        element.value = password;
    }, password);

    console.log('setting recaptcha g-response ...');
    await tab.$eval('#g-recaptcha-response', (element, token) => {
        element.value = token;
    }, token);

    console.log('submitting form .. ');
    await Promise.all([
        tab.click('#contentbox > form > div > div.tac.padding20px > button'),
        tab.waitForNavigation({ waitUntil: "networkidle0" })
    ]);

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

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

})();

You may note that we first solve the captcha and only then navigate to the page and paste the result. It works for Recaptcha as we already know the sitekey and lifetime of the token is 120 seconds.

The screenshot of the final page is saved to screenshot.png. If you wish to see what's going on in the browser, switch off the headless mode:

console.log('opening browser ..');
//const browser = await pup.launch();
const browser = await pup.launch({ headless: false });

Notes on extending your knowledge of Javascript ES6 and NodeJS.

You've learned how to bypass a form with Recaptcha V2 in it's simplest implementation way - with "g-recaptcha-response" textarea as part of the form. You may want to extend your knowledge:

Github

https://github.com/anti-captcha/solving-captcha-concepts/blob/master/tutorial1.js