Automatically solve captchas with NextCAPTCHA

Ivan BetancourtIvan Betancourt
2 min read

Add captcha solving to your automations with a few lines of code

If you engage in browser automation tasks like web scraping, you've likely encountered websites that interrupt your scripts with a captcha challenge.

Let NEXTCAPTCHA handle your captcha solving

If your Puppeteer or Playwright scripts get blocked by stubborn bot detectors, you’ll want to check this out. With a short snippet, it gives your automations the ability to solve any captcha.

Code Snippet

const puppeteer= require('puppeteer');
const axios = require('axios');
const url = '<https://www.google.com/recaptcha/api2/demo>';

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

    const page = await browser.newPage();

    await page.goto(url, {
        waitUntil: 'networkidle0',
    });

    const sitekey = await page.$eval('[data-sitekey]', el => el.getAttribute('data-sitekey'));

    if (sitekey) {
        const task = await createCaptchaTask(url, sitekey);
        if (task) {
            const result = await getTaskResult(task.taskId);
            if (!result.errorId) {
                await page.evaluate((value) => {

                    const textarea = document.querySelector('#g-recaptcha-response');
                    textarea.value = value;
                }, result.solution.gRecaptchaResponse);
                await page.click('#recaptcha-demo-submit');

                        // Continue...
            }
        }

    } else {
        console.log("find sitekey error");
    }

};

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

async function sleep(time = 500) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve();
        }, time)
    })
}
//
async function getTaskResult(taskId, tryTimes = 60 ) {
    try {
        console.log('tryTimes', tryTimes)
        const data = await axios.post('<https://api.nextcaptcha.com/getTaskResult>', {
            "clientKey": "CLIENT_KEY", // clientKey from NextCaptcha
            taskId
        });
                if (data.data.status === 'processing' && tryTimes >= 0) {
            await sleep();
            return getTaskResult(taskId, tryTimes-1);
        } else if (data.data.status === 'ready') {
            return data.data;
        } 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('createCaptchaTask error', e.message);
        return null;
    }
}

main();

Your scripts won’t need to do any extra work, all that’s needed is to wait until the NextCaptcha is solved with our solver API

also we have captcha solver sdk for Python,Go,C#. and we will Integrate more programming languages

captcha solver Python sdk: nextcaptcha-python

captcha solver Go sdk: nextcaptcha-go

captcha solver C# sdk: nextcaptcha-csharp

Happy automating

Email: support@nextcaptcha.com

Telegram: https://t.me/+wWJh5iON_I0xY2Vh

Website: https://nextcaptcha.com/

0
Subscribe to my newsletter

Read articles from Ivan Betancourt directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ivan Betancourt
Ivan Betancourt