How to Solve Captcha with Nodejs
Tags: captchasolving, nextcaptcha, bypasscaptcha, node
Published: 2024-03-14 01:59:30


### About reCAPTCHA v2

reCAPTCHA v2 is a CAPTCHA system developed by Google that aims to help websites distinguish between human users and automated programs (such as bots). It verifies a user's human identity by requiring them to click a checkbox before completing actions like submitting a form or logging in. reCAPTCHA v2 determines if a user is a genuine human by analyzing information such as their clicking behavior and browsing patterns.

### Type of reCAPTCHA v2

1. Checkbox
    
    As shown in the image, the "I'm not a robot" checkbox requires the user to click the checkbox indicating that the user is not a robot. This will either immediately pass the user (without a CAPTCHA) or challenge them to verify they are human.
    

![reCAPTCHA v2 checkbox type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhvtsd260elrlrztkz4t.gif)
    
2. Invisible
    
    As shown in the image, the invisible reCAPTCHA badge does not require the user to click a checkbox. Instead, it is directly invoked when the user clicks an existing button on the site, or it can be invoked via the JavaScript API. After the reCAPTCHA verification is completed, the integration requires a JavaScript callback. By default, only the most suspicious traffic will be prompted to solve a CAPTCHA.
    

![reCAPTCHA v2 Invisible type ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbofceebzlvjxzcwwf90.png)
    

In this article, we will unveil the mystery of CAPTCHAs and walk through the process of building a simple reCAPTCHA v2 solver using Node.js, step by step.

### Environment Setup

First, make sure you have Node.js installed on your system. Additionally, install the required libraries:

```bash
npm install axios
npm install cheerio
```

When the target webpage is a static page, we directly use `axios` to download the relevant webpage, and then use `cheerio` to parse the webpage and extract the relevant data needed to bypass reCAPTCHA v2.

Before we start the actual bypass, we need to register on the [NextCaptcha](https://nextcaptcha.com/) website. After registering an account, you can obtain an account key from the backend, which is the clientKey. Save it for later use.

After obtaining the relevant clientKey, we will formally begin our journey of bypassing reCAPTCHA v2.

Here we will use the official Demo as an example, and the target URL is [https://www.google.com/recaptcha/api2/demo](https://www.google.com/recaptcha/api2/demo)

### Obtain the HTML of the webpage

```jsx
const axios = require('axios');

async function getPageData(url) {
    try {
	    const data = await axios.get(url)
	    return data.data;  
    }  catch (e) {
        console.error('getPageData error', e.message);
        return null;
    }
}

getPageData('https://www.google.com/recaptcha/api2/demo');
```

### Obtain the reCAPTCHA related parameters

```jsx
const cheerio = require('cheerio');

function parserData(html) {
    try {
        const $ = cheerio.load(html);

        return $('[data-sitekey]').data('sitekey')

    } catch (e) {
        console.error('parserData error', e.message);
        return null;
    }
}

```

### Construct the bypass CAPTCHA request

```jsx
const axios = require('axios');

async function createCaptchaTask(url, siteKey, isInvisible) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/createTask', {
            "clientKey": "clientKey", // clientKey from NextCaptcha dashboard
            "task": {
                type: "RecaptchaV2TaskProxyless",
                websiteURL: url,
                websiteKey: siteKey,
                isInvisible
            }
        });
        return data.data;
    } catch (e) {
        console.error('createCaptchaTask error', e.message);
        return null;
    }
}
```

### Obtain the CAPTCHA solver task result

```jsx
const axios = require('axios');

async function sleep(time = 500) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve();
        }, time)
    })
}
async function getTaskResult(taskId) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/getTaskResult', {
            "clientKey": "clientKey", // clientKey from NextCaptcha
            taskId
        });
        if (data.data.status === 'processing') {
            await sleep();
            return getTaskResult(taskId)
        } else {
            console.error('createCaptchaTask errorCode', data.data.errorCode);
            console.error('createCaptchaTask errorDescription', data.data.errorDescription);
            return null;
        }
    } catch (e) {
        console.error('createCaptchaTask error', e.message);
        return null;
    }
}
```

### **Final Code**

```jsx
const axios = require('axios');
const cheerio = require('cheerio');

async function getPageData(url) {
    try {
	    const data = await axios.get(url)
	    return data.data;  
    }  catch (e) {
        console.error('getPageData error', e.message);
        return null;
    }
}

function parserData(html) {
    try {
        const $ = cheerio.load(html);

        return $('[data-sitekey]').data('sitekey')

    } catch (e) {
        console.error('parserData error', e.message);
        return null;
    }
}

async function createCaptchaTask(url, siteKey, isInvisible) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/createTask', {
            "clientKey": "clientKey", // clientKey from NextCaptcha dashboard
            "task": {
                type: "RecaptchaV2TaskProxyless",
                websiteURL: url,
                websiteKey: siteKey,
                isInvisible
            }
        });
        return data.data;
    } catch (e) {
        console.error('createCaptchaTask error', e.message);
        return null;
    }
}

async function sleep(time = 500) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve();
        }, time)
    })
}

async function getTaskResult(taskId, tryTimes = 60) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/getTaskResult', {
            "clientKey": "clientKey", // clientKey from NextCaptcha
            taskId
        });
         if (data.data.status === 'ready') {
            return data.data;
        } else if (data.data.status === 'processing' && tryTimes >= 0) {
            await sleep();
            return getTaskResult(taskId)
        } else {
            if (tryTimes < 0) {
                console.error('getTaskResult out of time');
            } else {
                console.error('getTaskResult errorCode', data.data.errorCode);
                console.error('getTaskResult errorDescription', data.data.errorDescription);
            }
            return null;
        }
    } catch (e) {
        console.error('getTaskResult error', e.message);
        return null;
    }
}

async function mian() {
    const url = 'https://www.google.com/recaptcha/api2/demo'
    const html = await getPageData(url);
    const sitekey = parserData(html);
    console.log(sitekey)
    const task = await createCaptchaTask(url, sitekey, false);
    const result = await getTaskResult(task.taskId);
    console.log(result)
}

mian()

```

The gRecaptchaResponse in the result obtained in the last step is the token solved by reCAPTCHA v2. We can use this token to submit it to the relevant interface of the website, so that we have completed the entire process of CAPTCHA bypass.

### Conclusion

Although the verification code service of [NextCaptcha](http://nextcaptcha.com/) is charged and each verification requires a certain amount of points, its price is relatively affordable compared to other similar services on the market. More importantly, its recognition accuracy is very high and it can crack the verification codes of many websites.

Currently, the website has recharge benefits. You can contact customer service to get discounts [https://t.me/nextcaptcha0](https://t.me/nextcaptcha0)