Solve hCaptcha in Playwright
Playwright stays in charge of the browser. The API call happens beside it, and the token is written into the page with evaluate.
Steps
- Read the sitekey from the widget in the page.
- Solve with the Python or Node client, using page.url as the url field.
- Evaluate a short script that fills both response textareas, then continue.
python
from playwright.sync_api import sync_playwright from vanishcaptcha import VanishCaptcha with sync_playwright() as p: page = p.chromium.launch().new_page() page.goto("https://example.com/login") sitekey = page.locator("[data-sitekey]").get_attribute("data-sitekey") solve = VanishCaptcha(api_key="nc_live_...").solve( type="hcaptcha", sitekey=sitekey, url=page.url, ) page.evaluate( """(token) => { for (const name of ["h-captcha-response", "g-recaptcha-response"]) { const el = document.querySelector('[name="' + name + '"]'); if (el) el.value = token; } }""", solve.token, )
node
import { chromium } from "playwright"; import { VanishCaptcha } from "vanishcaptcha"; const browser = await chromium.launch(); const page = await browser.newPage(); await page.goto("https://example.com/login"); const sitekey = await page.locator("[data-sitekey]").getAttribute("data-sitekey"); const solve = await new VanishCaptcha({ apiKey: process.env.VANISHCAPTCHA_KEY }).solve({ type: "hcaptcha", sitekey, url: page.url(), }); await page.evaluate((token) => { for (const name of ["h-captcha-response", "g-recaptcha-response"]) { const el = document.querySelector(`[name="${name}"]`); if (el) el.value = token; } }, solve.token);
Enterprise rqdata
If the widget carries rqdata, send type: "hcaptcha_enterprise" and that value. The token that comes back is what the enterprise sitekey expects. Fields are in the API reference.
Questions
Does this solve reCAPTCHA or Turnstile?
No. These guides are hCaptcha only. reCAPTCHA and Turnstile are not live on VanishCaptcha.
How long is the token good for?
About two minutes, and it can be submitted once. Request it immediately before the form goes out.