Automate captcha solving with Playwright and the CaptchaSonic API. Works with reCAPTCHA v2, reCAPTCHA v3, Cloudflare Turnstile, Geetest v4, AWS WAF, and image captchas.
# 1. Clone and install
git clone https://github.com/Captcha-Sonic/playwright-solver.git
cd captchasonic-playwright-examples
npm install
# 2. Install Playwright browsers
npx playwright install chromium
# 3. Configure your API key
cp .env.example .env
# Edit .env and add your API key
# 4. Run any example
npm run recaptcha-v2Get your API key at captchasonic.com.
| Captcha | Token Method | Script Method | Run Command |
|---|---|---|---|
| reCAPTCHA v2 | ✅ | ✅ | npm run recaptcha-v2 |
| reCAPTCHA v3 | ✅ | — | npm run recaptcha-v3 |
| Cloudflare Turnstile | ✅ | — | npm run turnstile |
| Geetest v4 | ✅ | ✅ | npm run geetest |
| AWS WAF | ✅ | ✅ | npm run aws-waf |
| Image Captcha (OCR) | ✅ | — | npm run image-captcha |
| Popular Captcha | ✅ | ✅ | npm run popularcaptcha |
Uses the CaptchaSonic API to solve captchas server-side — no visible browser interaction needed.
Your script → CaptchaSonic API → returns token → inject into page → submit
import { chromium } from 'playwright';
import { CaptchaSonic } from 'captchasonic';
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com/login');
const client = new CaptchaSonic(process.env.CAPTCHASONIC_API_KEY);
const result = await client.solveRecaptchaV2Token({
websiteURL: 'https://example.com/login',
websiteKey: '6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-',
});
await page.evaluate((token) => {
(document.querySelector('#g-recaptcha-response') as HTMLTextAreaElement).value = token;
document.querySelector('form')!.submit();
}, result.solution.gRecaptchaResponse);Best for: headless automation, CI/CD pipelines, maximum speed (5–15s)
Injects lightweight JS scripts into the page that detect, extract, and click captcha challenges using the CaptchaSonic image classification API.
Inject script → detect captcha → extract images → SDK classifies → inject clicks
import { ScriptSolver } from '../shared/solver';
const solver = new ScriptSolver(challengeFrame, client);
const result = await solver.solve('popularcaptcha', { timeout: 120, maxRetries: 5 });Best for: Popular Captcha, Geetest, AWS WAF, and any visual challenge captcha
| Token Method | Script Method | |
|---|---|---|
| How it works | API returns bypass token | JS extracts tiles → SDK classifies → JS clicks |
| Speed | ⚡ 5–15 seconds | 🐢 30–60 seconds per round |
| Best for | reCAPTCHA v2/v3, Turnstile | Popular Captcha, Geetest, AWS WAF |
| Recommended | ✅ Start here | When token method is unavailable |
If you are an AI agent or LLM-based tool integrating CaptchaSonic into a Playwright workflow, follow this protocol:
┌──────────────────┐ ┌────────────────┐ ┌────────────────┐
│ Playwright Page │────▶│ ScriptSolver │────▶│ CaptchaSonic │
│ (browser ctrl) │◀────│ (orchestrator)│◀────│ SDK (gRPC/HTTP) │
└──────────────────┘ └────────────────┘ └────────────────┘
↕ ↕
Page / frames JS scripts (detect/
extract/inject)
- Initialize SDK:
const client = new CaptchaSonic(apiKey) - Navigate:
await page.goto(targetUrl) - Solve: Call the appropriate
client.solve*Token({ websiteURL, websiteKey })method - Extract token:
result.solution.gRecaptchaResponse(or.tokenor.['cf-turnstile-response']) - Inject: Use
shared/helpers.ts→injectRecaptchaToken(page, token)orinjectTurnstileToken(page, token) - Submit form:
await page.click('button[type="submit"]')
- Initialize SDK:
const client = new CaptchaSonic(apiKey) - Navigate to target page and wait for captcha iframe
- Find challenge frame: Use
page.frames()to locate the challenge iframe (look forchallengein URL) - Create solver:
const solver = new ScriptSolver(challengeFrame, client) - Solve:
const result = await solver.solve('popularcaptcha', { timeout: 120, maxRetries: 5 }) - Check result:
result.solved→truemeans captcha is complete
The solve() method runs this loop internally:
1. inject(script.js) → adds global object (e.g. CaptchaSonic_PopularCaptcha)
2. detect() → returns {found, type, solved}
3. extract() → returns {images[], question, questionType}
4. SDK call → sends images to CaptchaSonic API → returns answers
5. inject(solution) → clicks correct tiles / coordinates in DOM
6. Check solved → if newChallenge, repeat from step 3
captchaType |
JS Script | SDK Method |
|---|---|---|
'popularcaptcha' |
popularcaptcha.js |
solvePopularCaptcha() |
'recaptcha_v2' |
recaptcha-v2.js |
solveRecaptchaV2() |
'geetest' |
geetest.js |
solveGeetest() |
'aws_waf' |
aws-waf.js |
solveAwsWaf() |
| File | Purpose |
|---|---|
shared/solver.ts |
ScriptSolver class — main orchestration logic |
shared/helpers.ts |
API key loading, token extraction, injection utilities |
scripts/*.js |
Vendored JS scripts for DOM detection/extraction/injection |
examples/*/token-method.ts |
Token-based solve (fastest, start here) |
examples/*/script-method.ts |
Script-based solve (visual captchas) |
# Token method (API-based)
npm run recaptcha-v2
npm run recaptcha-v3
npm run turnstile
npm run geetest
npm run aws-waf
npm run image-captcha
npm run popularcaptcha
# Script method (JS injection)
npm run recaptcha-v2:script
npm run geetest:script
npm run aws-waf:script
npm run popularcaptcha:scriptcp .env.example .envCAPTCHASONIC_API_KEY=sonic_your_key_here| Variable | Required | Description |
|---|---|---|
CAPTCHASONIC_API_KEY |
✅ | Your CaptchaSonic API key (sonic_xxx) |
Each example targets a public demo page. To use on your own site:
- Open any
token-method.ts - Change
SITE_URLto your target page - Change
SITE_KEYto the captcha key on that page - Run the script
const SITE_URL = 'https://yoursite.com/login';
const SITE_KEY = 'your-site-key-here';captchasonic-playwright-examples/
├── scripts/ # Vendored JS captcha scripts
│ ├── popularcaptcha.js
│ ├── recaptcha-v2.js
│ ├── geetest.js
│ └── aws-waf.js
├── shared/
│ ├── helpers.ts # Reusable utilities
│ └── solver.ts # ScriptSolver bridge class
├── examples/
│ ├── recaptcha-v2/
│ │ ├── token-method.ts
│ │ └── script-method.ts
│ ├── recaptcha-v3/
│ │ └── token-method.ts
│ ├── turnstile/
│ │ └── token-method.ts
│ ├── geetest/
│ │ ├── token-method.ts
│ │ └── script-method.ts
│ ├── aws-waf/
│ │ ├── token-method.ts
│ │ └── script-method.ts
│ ├── image-captcha/
│ │ └── token-method.ts
│ └── popularcaptcha/
│ ├── token-method.ts
│ └── script-method.ts
├── package.json
└── tsconfig.json
- Node.js ≥ 18
- Playwright — installed automatically via
npm install - CaptchaSonic API key — get one here
- CaptchaSonic Documentation
- CaptchaSonic Dashboard
- Playwright Documentation
- Puppeteer TypeScript Examples
- Selenium Python Examples
MIT — free to use in commercial projects.