Tutoriais de API

Resolva Grid Image CAPTCHA com Node.js e CaptchaAI

Imagem de grade CAPTCHAs apresentam uma grade 3×3 ou 4×4 com uma instrução como “selecione todos os quadrados com semáforos”. Este tutorial mostra como resolvê-los em Node.js usandoCaptchaAIe Puppeteer.


Pré-requisitos

Artigo Valor
Chave de API CaptchaAI Decaptchaai.com
Node.js 14+
Bibliotecas axios, puppeteer

Etapa 1: capturar a imagem da grade

const puppeteer = require('puppeteer');
const fs = require('fs');

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com/page-with-recaptcha');

// Switch to the reCAPTCHA challenge iframe
const frames = page.frames();
const challengeFrame = frames.find((f) => f.url().includes('recaptcha/api2/bframe'));

// Get the instruction text
const instruction = await challengeFrame.$eval(
  '.rc-imageselect-desc-no-canonical',
  (el) => el.textContent.trim()
);

// Screenshot the grid
const grid = await challengeFrame.$('.rc-imageselect-target');
await grid.screenshot({ path: 'grid.png' });

Etapa 2: enviar para CaptchaAI

const axios = require('axios');
const FormData = require('form-data');

const API_KEY = 'YOUR_API_KEY';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

const form = new FormData();
form.append('key', API_KEY);
form.append('method', 'post');
form.append('grid_size', '3x3');
form.append('img_type', 'recaptcha');
form.append('instructions', instruction);
form.append('json', '1');
form.append('file', fs.createReadStream('grid.png'));

const { data: submitData } = await axios.post('https://ocr.captchaai.com/in.php', form, {
  headers: form.getHeaders(),
});

if (submitData.status !== 1) throw new Error(submitData.request);
const taskId = submitData.request;
console.log(`Task submitted: ${taskId}`);

Passo 3: Pesquise a solução

await sleep(5000);

let cellsToClick;
for (let i = 0; i < 30; i++) {
  const { data: pollData } = await axios.get('https://ocr.captchaai.com/res.php', {
    params: { key: API_KEY, action: 'get', id: taskId, json: 1 },
  });

  if (pollData.status === 1) {
    cellsToClick = JSON.parse(pollData.request);
    console.log('Click cells:', cellsToClick);
    break;
  }
  if (pollData.request !== 'CAPCHA_NOT_READY') {
    throw new Error(pollData.request);
  }
  await sleep(5000);
}

Etapa 4: clique nos blocos corretos

const tiles = await challengeFrame.$$('.rc-imageselect-tile');

for (const cellNum of cellsToClick) {
  await tiles[cellNum - 1].click();
  await sleep(300);
}

// Click verify
await challengeFrame.click('#recaptcha-verify-button');
console.log(`Solved: clicked tiles ${JSON.stringify(cellsToClick)}`);
await browser.close();

Resultado esperado:

Click cells: [1, 3, 6, 9]
Solved: clicked tiles [1,3,6,9]

Perguntas frequentes

Isso funciona com grades 4×4?

Sim. Defina grid_size como 4x4 nos parâmetros de solicitação.

Posso usar o Playwright em vez do Puppeteer?

Sim. As chamadas da API CaptchaAI são as mesmas – apenas o código de automação do navegador muda.

E se o CAPTCHA for atualizado com novas imagens?

Alguns desafios do reCAPTCHA carregam novos blocos. Pode ser necessário capturar e enviar novamente para cada rodada.


Guias relacionados


Comece a resolver Grid Image CAPTCHAs com CaptchaAI →

Os comentários estão desativados para este artigo.