Search code examples
puppeteerrecaptchaapify

Why is Puppeteer-Extra's solveRecaptchas() "not a function"?


I am attempting to use Puppeteer and puppeteer-extra-plugin-recaptcha through Apify to solve the Recaptcha on a login page.

In time, I will want to do the full login. For now, I am attempting to do the Recaptcha solution, using Google's demo. puppeteer-extra-plugin-recaptcha taps up the 2captcha service via API key to do the solving.

Unfortunately, in my code, the important solveRecaptchas() throws an error:

TypeError: page.solveRecaptchas is not a function

Full error:

2021-10-17T10:04:54.502Z ERROR PuppeteerCrawler: handleRequestFunction failed, reclaiming failed request back to the list or queue {"url":"https://www.google.com/recaptcha/api2/demo","retryCount":1,"id":"ylMuptksDTSkKJl"}
2021-10-17T10:04:54.504Z   TypeError: page.solveRecaptchas is not a function
2021-10-17T10:04:54.505Z       at PuppeteerCrawler.handlePageFunction (/home/myuser/main.js:85:28)
2021-10-17T10:04:54.507Z       at PuppeteerCrawler._handleRequestFunction (/home/myuser/node_modules/apify/build/crawlers/browser_crawler.js:324:52)
2021-10-17T10:04:54.509Z       at processTicksAndRejections (node:internal/process/task_queues:96:5)
2021-10-17T10:04:54.511Z       at async PuppeteerCrawler._runTaskFunction (/home/myuser/node_modules/apify/build/crawlers/basic_crawler.js:431:13)
2021-10-17T10:04:54.513Z       at async AutoscaledPool._maybeRunTask (/home/myuser/node_modules/apify/build/autoscaling/autoscaled_pool.js:408:17)

My code:

/**************************************/
/*          Setup requires            */
/**************************************/
const Apify = require("apify");
const puppeteer = require("puppeteer-extra");
const PuppeteerExtraPluginRecaptcha = require("puppeteer-extra-plugin-recaptcha");
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
// puppeteer.use(StealthPlugin()); // Should this be used?



/**************************************/
/*          Use Recaptcha             */
/**************************************/
function addPlugins() {
  const captchaOptions = {
    provider: {
      id: "2captcha",
      token: "my-2captcha-api-key",
    },
    solveInactiveChallenges: true,
    visualFeedback: true,
  };
  puppeteer.use(PuppeteerExtraPluginRecaptcha(captchaOptions));
}



/**************************************/
/*           Through Apify?           */
/**************************************/
Apify.main(
    async () => {

    /**************************************/
    /*             Cue up URL             */
    /**************************************/
    // Is this what to do and where to do it?
    // cf. https://reposhub.com/nodejs/http/apifytech-apify-js.html
    const requestQueue = await Apify.openRequestQueue();
    await requestQueue.addRequest(
        {
            url: "https://www.google.com/recaptcha/api2/demo",
        }
    );
    // const pseudoUrls = [new Apify.PseudoUrl('https://www.iana.org/[.*]')];


    /**************************************/
    /*         Puppeteer Crawler          */
    /**************************************/
    const crawler = new Apify.PuppeteerCrawler(
        {
            requestQueue,
            launchContext: {
            launcher: puppeteer,
            launchOptions: {
                devtools: false,
                headless: false,
                ignoreHTTPSErrors: true,
                defaultViewport: { width: 1366, height: 768 },
                timeout: 60 * 1000,
                args: [
                "--no-sandbox",
                "--disable-web-security",
                "--disable-setuid-sandbox",
                "--disable-features=IsolateOrigins,site-per-process",
                "--flag-switches-begin --disable-site-isolation-trials --flag-switches-end",
                ],
            },
        },
        handlePageFunction: async ({ request, page }) => {

            // ...some code
            // Should URL be enqueued here, not above?
           
            // solve captcha (if one exists)
            await page.solveRecaptchas(); // https://github.com/berstend/puppeteer-extra/issues/184
            
            // additional code

        },
    });

    await crawler.run();

    }
);

Possible causes I have read about include:


Solution

  • This might be an omission from the example you provided, but it seems that you're not calling the addPlugins() function anywhere and therefore the plugin is not used at all.