Giter VIP home page Giter VIP logo

solving-recaptcha-using-puppeteer's Introduction

Sample code to automate reCAPTCHA solution with Puppeteer

Description

This code example demonstrates how to automate solving a reCAPTCHA using the Puppeteer library. To captcha solving, this example uses the 2captcha.com service. This example solves the captcha located on the page https://2captcha.com/demo/recaptcha-v2. You need to have a 2captcha.com account for the example to work.

How to start:

Cloning

git clone https://github.com/dzmitry-duboyski/solving-recaptcha-using-puppeteer.git

Install dependencies

npm install

Set your APIKEY in .env file

APIKEY is specified in the personal account 2captcha.com. Before copying the APIKEY, check the selected role, the "developer" role must be installed in the personal account.

Start

npm run start

How it works

import puppeteer from "puppeteer";
const APIKEY = process.env.APIKEY;
import { Solver } from "2captcha-ts";
const solver = new Solver(APIKEY);

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
  });

  const page = await browser.newPage();
  await page.setViewport({ width: 1080, height: 1024 });

  await page.goto("https://2captcha.com/demo/recaptcha-v2");
  await page.waitForSelector(".g-recaptcha");

  // Extract the `sitekey` parameter from the page.
  const sitekey = await page.evaluate(() => {
    return document.querySelector(".g-recaptcha").getAttribute("data-sitekey");
  });

  // Get actual page url
  const pageurl = await page.url();

  // Submitting the captcha for solution to the service
  const res = await solver.recaptcha({
    pageurl: pageurl,
    googlekey: sitekey,
  });

  console.log(res);

  // Getting a captcha response including a captcha answer
  const captchaAnswer = res.data;

  // Use captcha answer
  const setAnswer = await page.evaluate((captchaAnswer) => {
    // It is not necessary to make this block visible, it is done here for clarity.
    document.querySelector("#g-recaptcha-response").style.display = "block";
    document.querySelector("#g-recaptcha-response").value = captchaAnswer;
  }, captchaAnswer);

  // Press the button to check the result.
  await page.click('button[type="submit"]');

  // Check result
  await page.waitForSelector("form div pre code");

  const resultBlockSelector = "form div pre code";
  let statusSolving = await page.evaluate((selector) => {
    return document.querySelector(selector).innerText;
  }, resultBlockSelector);

  statusSolving = JSON.parse(statusSolving);
  if (statusSolving.success) {
    console.log("Captcha solved successfully!!!");
  }

  await page.waitForTimeout(5000);
  browser.close();
})();

The source code is available in the file index.js

Additional information:

solving-recaptcha-using-puppeteer's People

Contributors

dzmitry-duboyski avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.