Um usuário clica em “Enviar” e um modal aparece com um desafio CAPTCHA. A chave do site não está na origem da página inicial - ela carrega dinamicamente quando o modal é aberto. Seu script de automação precisa acionar o modal, aguardar a renderização do CAPTCHA, extrair parâmetros, resolver e injetar o token antes que o modal expire ou a sessão do usuário expire.
Padrões modais CAPTCHA
| Padrão | Gatilho | Desafio |
|---|---|---|
| Modo de login | Clique no botão "Entrar" | CAPTCHA carrega dentro da div de sobreposição |
| Intersticial antibot | Automático após comportamento suspeito | Página de blocos modais em tela cheia |
| Confirmação de check-out | Enviar formulário de pagamento | Modal aparece para verificação |
| Diálogo de limite de taxa | Muitas solicitações detectadas | Modal com porta CAPTCHA |
| Consentimento de cookies + CAPTCHA | Primeira visita | CAPTCHA incorporado na caixa de diálogo de consentimento |
Python: manipulador modal CAPTCHA do dramaturgo
import requests
import time
from playwright.sync_api import sync_playwright
API_KEY = "YOUR_API_KEY"
SUBMIT_URL = "https://ocr.captchaai.com/in.php"
RESULT_URL = "https://ocr.captchaai.com/res.php"
def solve_captcha(sitekey, pageurl, method="userrecaptcha"):
"""Submit and poll a CAPTCHA."""
params = {
"key": API_KEY,
"method": method,
"json": 1,
}
if method == "userrecaptcha":
params["googlekey"] = sitekey
params["pageurl"] = pageurl
elif method == "turnstile":
params["sitekey"] = sitekey
params["pageurl"] = pageurl
resp = requests.post(SUBMIT_URL, data=params, timeout=30).json()
if resp.get("status") != 1:
raise RuntimeError(f"Submit failed: {resp.get('request')}")
task_id = resp["request"]
for _ in range(60):
time.sleep(5)
poll = requests.get(RESULT_URL, params={
"key": API_KEY, "action": "get",
"id": task_id, "json": 1,
}, timeout=15).json()
if poll.get("request") == "CAPCHA_NOT_READY":
continue
if poll.get("status") == 1:
return poll["request"]
raise RuntimeError(f"Solve failed: {poll.get('request')}")
raise RuntimeError("Timeout")
def detect_modal_captcha(page):
"""
Detect CAPTCHA inside a visible modal/dialog.
Returns (sitekey, method) or (None, None).
"""
return page.evaluate("""
() => {
// Find visible modals
const modalSelectors = [
'.modal.show',
'.modal[style*="display: block"]',
'dialog[open]',
'[role="dialog"]:not([aria-hidden="true"])',
'.overlay.visible',
'.popup.active',
'[class*="modal"][class*="open"]',
];
let modal = null;
for (const sel of modalSelectors) {
const el = document.querySelector(sel);
if (el && el.offsetParent !== null) {
modal = el;
break;
}
}
// If no modal found, search entire document
const searchRoot = modal || document;
// Check for reCAPTCHA
const recaptcha = searchRoot.querySelector('.g-recaptcha[data-sitekey]');
if (recaptcha) {
return { sitekey: recaptcha.dataset.sitekey, method: 'userrecaptcha' };
}
// Check for Turnstile
const turnstile = searchRoot.querySelector('.cf-turnstile[data-sitekey]');
if (turnstile) {
return { sitekey: turnstile.dataset.sitekey, method: 'turnstile' };
}
// Check for hCaptcha
const hcaptcha = searchRoot.querySelector('.h-captcha[data-sitekey]');
if (hcaptcha) {
return { sitekey: hcaptcha.dataset.sitekey, method: 'hcaptcha' };
}
return null;
}
""")
def inject_token_in_modal(page, token, method="userrecaptcha"):
"""Inject token into the CAPTCHA inside the modal."""
if method == "userrecaptcha":
page.evaluate("""
(token) => {
// Find response textarea (may be inside modal)
const textareas = document.querySelectorAll('#g-recaptcha-response, [name="g-recaptcha-response"]');
textareas.forEach(ta => {
ta.value = token;
ta.style.display = 'block';
});
// Trigger callback
if (typeof ___grecaptcha_cfg !== 'undefined') {
Object.values(___grecaptcha_cfg.clients).forEach(client => {
Object.values(client).forEach(val => {
if (val && typeof val === 'object') {
Object.values(val).forEach(v => {
if (v && typeof v.callback === 'function') v.callback(token);
});
}
});
});
}
}
""", token)
elif method == "turnstile":
page.evaluate("""
(token) => {
const inputs = document.querySelectorAll('[name="cf-turnstile-response"]');
inputs.forEach(inp => { inp.value = token; });
if (typeof window.turnstileCallback === 'function') {
window.turnstileCallback(token);
}
}
""", token)
def handle_modal_captcha(page, trigger_selector=None, timeout=10000):
"""
Full workflow: trigger modal, detect CAPTCHA, solve, inject.
"""
# Step 1: Trigger the modal if needed
if trigger_selector:
print(f"Clicking trigger: {trigger_selector}")
page.click(trigger_selector)
# Step 2: Wait for modal to become visible
print("Waiting for modal...")
modal_selectors = [
".modal.show",
"dialog[open]",
'[role="dialog"]:not([aria-hidden="true"])',
".popup.active",
]
modal_visible = False
for selector in modal_selectors:
try:
page.wait_for_selector(selector, timeout=timeout)
modal_visible = True
print(f" Modal detected: {selector}")
break
except Exception:
continue
if not modal_visible:
print(" No modal detected")
return None
# Step 3: Wait for CAPTCHA to render inside modal
time.sleep(2) # Brief pause for dynamic CAPTCHA loading
captcha_info = detect_modal_captcha(page)
if not captcha_info:
print(" No CAPTCHA found in modal")
return None
sitekey = captcha_info["sitekey"]
method = captcha_info["method"]
print(f" Found {method} CAPTCHA: {sitekey[:20]}...")
# Step 4: Solve via CaptchaAI
token = solve_captcha(sitekey, page.url, method)
print(f" Solved: {token[:30]}...")
# Step 5: Inject token
inject_token_in_modal(page, token, method)
print(" Token injected")
return token
def main():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://example.com")
page.wait_for_load_state("networkidle")
# Handle CAPTCHA that appears in login modal
token = handle_modal_captcha(
page,
trigger_selector="button#login-btn",
timeout=10000,
)
if token:
# Fill form fields inside modal
page.fill('dialog input[name="email"]', "user@example.com")
page.fill('dialog input[name="password"]', "password123")
# Submit modal form
page.click('dialog button[type="submit"]')
page.wait_for_load_state("networkidle")
browser.close()
main()
JavaScript: manipulador modal do titereiro
const puppeteer = require("puppeteer");
const API_KEY = "YOUR_API_KEY";
const SUBMIT_URL = "https://ocr.captchaai.com/in.php";
const RESULT_URL = "https://ocr.captchaai.com/res.php";
async function solveCaptcha(sitekey, pageurl, method = "userrecaptcha") {
const body = new URLSearchParams({ key: API_KEY, method, json: "1" });
if (method === "userrecaptcha") { body.set("googlekey", sitekey); body.set("pageurl", pageurl); }
else if (method === "turnstile") { body.set("sitekey", sitekey); body.set("pageurl", pageurl); }
const resp = await (await fetch(SUBMIT_URL, { method: "POST", body })).json();
if (resp.status !== 1) throw new Error(`Submit: ${resp.request}`);
const taskId = resp.request;
for (let i = 0; i < 60; i++) {
await new Promise((r) => setTimeout(r, 5000));
const url = `${RESULT_URL}?key=${API_KEY}&action=get&id=${taskId}&json=1`;
const poll = await (await fetch(url)).json();
if (poll.request === "CAPCHA_NOT_READY") continue;
if (poll.status === 1) return poll.request;
throw new Error(`Solve: ${poll.request}`);
}
throw new Error("Timeout");
}
async function waitForModal(page, timeout = 10000) {
const selectors = [".modal.show", "dialog[open]", '[role="dialog"]', ".popup.active"];
for (const sel of selectors) {
try {
await page.waitForSelector(sel, { visible: true, timeout });
return sel;
} catch {}
}
return null;
}
async function detectModalCaptcha(page) {
return page.evaluate(() => {
const checks = [
{ sel: ".g-recaptcha[data-sitekey]", method: "userrecaptcha" },
{ sel: ".cf-turnstile[data-sitekey]", method: "turnstile" },
];
for (const { sel, method } of checks) {
const el = document.querySelector(sel);
if (el && el.dataset.sitekey) return { sitekey: el.dataset.sitekey, method };
}
return null;
});
}
async function handleModalCaptcha(page, triggerSelector) {
// Trigger modal
if (triggerSelector) await page.click(triggerSelector);
// Wait for modal
const modalSel = await waitForModal(page);
if (!modalSel) { console.log("No modal found"); return null; }
console.log(`Modal visible: ${modalSel}`);
// Wait for CAPTCHA render
await new Promise((r) => setTimeout(r, 2000));
const info = await detectModalCaptcha(page);
if (!info) { console.log("No CAPTCHA in modal"); return null; }
console.log(`Found ${info.method}: ${info.sitekey.substring(0, 20)}...`);
const token = await solveCaptcha(info.sitekey, page.url(), info.method);
console.log(`Solved: ${token.substring(0, 30)}...`);
// Inject token
await page.evaluate((t, method) => {
if (method === "userrecaptcha") {
document.querySelectorAll("#g-recaptcha-response").forEach((el) => { el.value = t; });
} else if (method === "turnstile") {
document.querySelectorAll('[name="cf-turnstile-response"]').forEach((el) => { el.value = t; });
}
}, token, info.method);
return token;
}
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto("https://example.com", { waitUntil: "networkidle2" });
const token = await handleModalCaptcha(page, "button#login-btn");
if (token) {
await page.type('dialog input[name="email"]', "user@example.com");
await page.click('dialog button[type="submit"]');
await page.waitForNavigation();
}
await browser.close();
})();
Considerações sobre tempo modal
| Fator | Impacto | Mitigação |
|---|---|---|
| Tempo limite de fechamento automático modal | Modal pode fechar antes da conclusão da resolução | Comece a resolver imediatamente após a detecção |
| Expiração da sessão durante a resolução | A sessão do servidor expira em espera modal | Mantenha a sessão ativa com pulsação em segundo plano |
| Atraso de renderização CAPTCHA no modal | O widget leva de 1 a 3 segundos para carregar no modal | Aguarde 2s após o modal ficar visível antes de extrair a chave do site |
| Expiração do token durante o preenchimento do formulário | O token expira ao preencher o formulário modal | Resolva o CAPTCHA por último, após preencher os demais campos |
Solução de problemas
| Problema | Causa | Correção |
|---|---|---|
| Modal detectado, mas nenhum CAPTCHA encontrado | CAPTCHA carrega de forma assíncrona após a abertura do modal | Aumentar o tempo de espera; use MutationObserver para detectar inserção de widget |
| Token injetado mas modal não fecha | Função de retorno de chamada não acionada | Encontre e invoque o retorno de chamada CAPTCHA explicitamente |
| Modal fecha durante a resolução | Tempo limite de dispensa automática | Desative o tempo limite modal via JS: clearTimeout() no temporizador modal |
| Chave do site CAPTCHA diferente a cada vez | Modal gera instâncias CAPTCHA dinâmicas | Sempre extraia o sitekey atualizado do DOM modal, nunca armazene em cache |
| O gatilho de clique não abre modal | Elemento não interativo ou atrás de sobreposição | Use page.dispatchEvent ou espere que o elemento seja clicável |
Perguntas frequentes
Como faço para detectar um modal que abre automaticamente sem um gatilho de clique?
Use um MutationObserver para observar novos elementos que aparecem no DOM. Configure-o antes de navegar para a página. Quando um elemento modal é adicionado e se torna visível, seu observador é acionado e você pode iniciar o fluxo de detecção CAPTCHA.
E se o CAPTCHA estiver dentro de um iframe modal?
Se o modal contiver um iframe com CAPTCHA, combine esta abordagem com o tratamento de iframe. Depois de detectar o modal, mude para o contexto iframe dentro do modal para extrair a chave do site.
Devo preencher os campos do formulário antes ou depois de resolver o CAPTCHA?
Antes. Preencha todos os outros campos do formulário primeiro e depois resolva o CAPTCHA por último. Isso minimiza o tempo entre a obtenção do token e o envio do formulário, reduzindo o risco de expiração.
Artigos relacionados
- Como resolver o retorno de chamada do Recaptcha V2 usando API
- Comparação de Geetest vs Cloudflare Turnstile
- Torniquete Recaptcha V2 no mesmo local
Próximas etapas
Lide com CAPTCHAs em modais pop-up perfeitamente -obtenha sua chave API CaptchaAIe implementar detecção modal.
Guias relacionados:
- Lidando com vários CAPTCHAs em uma única página
- Manipulação de Shadow DOM CAPTCHA
- Extração de iframe CAPTCHA: quadros aninhados