How can I get a reCAPTCHA image using puppeteer?

I need to build a web scraper in this government siteTRANSITO-MG, and it has a reCAPTCHA. I found this tool that solves captchas NopeCHA, but to send to the api, I need the image src, the grid and the task, the three of them can be found by inspecting element, but when I try to do that via puppeteer, it returns me a different html than the inspect element one

That is my code so far:

import puppeteer from 'puppeteer';
import { environment } from '../../../types/environment';
import { env } from '../../../utils/env';
const { Configuration, NopeCHAApi } = require("nopecha")
const config = new Configuration({
    apiKey: env.NOPECHA_API_KEY,
})
const nopecha = new NopeCHAApi(config);
(async () => {
    // Launch the browser and open a new blank page
    const browser = await puppeteer.launch({ headless: false, args: ['--lang=en-US,en'] });
    const page = await browser.newPage();
    // Navigate the page to a URL
    await page.goto('``https://transito.mg.gov.br/veiculos/situacao-do-veiculo/consultar-situacao-do-veiculo``');
    // Set screen size
    await page.setViewport({ width: 1080, height: 1024 });
    // Fill placa
    await page.type('#placa', 'ABC1234');
    // Fill chassi
    await page.type('#chassi', '12345678901234567');
    // Click captcha
    await page.waitForSelector("iframe")
    const elementHandle1 = await page.$("#content > form > div.g-recaptcha.mb-3 > div > div > iframe")
    const iframe1 = await elementHandle1?.contentFrame()
    await iframe1?.click("#recaptcha-anchor > div.recaptcha-checkbox-border")
    let image, imageUrl, grid
    page.waitForNetworkIdle()
    await page.waitForSelector("iframe[title="o desafio reCAPTCHA expira em dois minutos"]")
    const elementHandle2 = await page.$("iframe[title="o desafio reCAPTCHA expira em dois minutos"]")
    const iframe2 = await elementHandle2?.contentFrame()
    page.waitForNetworkIdle()
    console.log('waiting iframe2 content')
    const iframe2HTML = await iframe2?.content()
    console.log(iframe2HTML)
})();

And the iframe html it returns is this (I ommited the style tag because it is irrelevant i believe):

<body>
  <input type="hidden" id="recaptcha-token">
  <script type="text/javascript" nonce="">
    recaptcha.frame.Main.init("[\x22finput\x22,null,[\x22conf\x22,null,\x226LfVpnIUAAAAAHkISk6Z6juZcsUx6hbyJGwfnfPL\x22,0,null,null,0.75,null,[21,73,87,41,43,42,83]]]");
  </script>
  <div></div>
</body>

Leave a Comment