Skip to content
CaptchaSonic

Playwright Integration

Playwright + CaptchaSonic

Automate captcha solving in your Playwright test suites and scraping pipelines.

npm

Quick Install

npm install captchasonic playwright

01

Initialize the CaptchaSonic client

Import and configure the SDK with your API key.

import { chromium } from 'playwright';
import { CaptchaSonic } from 'captchasonic';

// Solves over a native gRPC connection (no HTTP overhead)
const client = new CaptchaSonic(process.env.CAPTCHASONIC_API_KEY);

02

Navigate to the target page

Launch a browser and navigate to the page with the captcha.

const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://example.com/login');

// Wait for the captcha iframe to appear
await page.waitForSelector('iframe[src*="recaptcha"]');

03

Extract the sitekey and solve

Get the captcha sitekey from the page and send it to CaptchaSonic for solving.

// Extract the sitekey
const sitekey = await page.getAttribute(
  '[data-sitekey]', 'data-sitekey'
);

// Solve the captcha via CaptchaSonic over gRPC
const result = await client.solveRecaptchaV2Token({
  websiteURL: page.url(),
  websiteKey: sitekey,
});

const token = result.solution.gRecaptchaResponse;
console.log('Token:', token);

04

Inject the token and submit

Set the captcha response token and submit the form.

// Inject the solved token
await page.evaluate((token) => {
  document.querySelector('#g-recaptcha-response')
    .value = token;
}, token);

// Submit the form
await page.click('button[type="submit"]');
await page.waitForNavigation();

console.log('Successfully bypassed captcha!');
await browser.close();

Links

Related Resources

Ready to automate?

Get your API key and start solving captchas in minutes. Free $0.50 bonus credits included.

Get API Key