How to bypass captcha in Puppeteer: capture, solve, click
To bypass a captcha in Puppeteer, let the page display the challenge, capture its element with a screenshot, send the picture to a solver API and replay the answer with the mouse of the page. Puppeteer gives the image and the position of the element; the solver returns where to click. This page explains the capture, the scale, the clicks and the slider, then gives the complete Node.js script.
Preview: the API described here is in public beta. Endpoint names, fields and codes can still change until the end of the beta, and no API key is distributed yet.
How do you capture the captcha element in Puppeteer?
page.waitForSelector with the option visible returns a handle on the challenge once it is displayed. el.screenshot captures that element alone, as PNG bytes, and el.boundingBox gives its position and its size in CSS pixels. The example wraps the bytes in a Buffer, reads the width from the PNG and encodes the image in base64 for the image field of the task.
| Item | Value |
|---|---|
| Runs on | Node.js 18 or newer, Puppeteer 22 or newer |
| Install | npm i puppeteer |
| File | solve_puppeteer.js |
| Command | node solve_puppeteer.js |
| Browser | Chrome for Testing, downloaded at install |
How do you click the points a solver returns in Puppeteer?
Convert each point from image pixels to page coordinates, then drive page.mouse. The scale is the PNG width, read with readUInt32BE at byte 16, divided by box.width. A point becomes box.x + x / and box.y + y /: the corner of the element plus the distance inside it.
page.mouse.movewithstepsset to 20 sends twenty intermediate moves instead of one jump.page.mouse.downthenpage.mouse.upmake the click, as two separate events.pausewaits 50 to 150 milliseconds, at random, between the gestures.
How do you drag a slider captcha in Puppeteer?
Capture the slider, submit it as a binance task and divide the returned offset by the scale. The example then takes the centre of the handle from its boundingBox, moves the mouse there in 10 steps and presses the button with page.mouse.down. The drag is cut into 10 to 15 moves, each shifted up or down by at most 2 pixels, the last one back on the starting line; page.mouse.up releases the handle.
Which browser does the Puppeteer example launch?
Chrome for Testing, the build that npm i puppeteer downloads into its cache and that matches the installed Puppeteer version. puppeteer.launch with headless set to false opens it in a visible window. To drive the Chrome installed on the machine instead, pass channel with the value chrome, or executablePath with the path of the binary.
PAGE_URLis the page that shows the captcha.CAPTCHA_SELECTORis the CSS selector of the challenge element.browser.closeends the session and closes the window.
What does a complete Puppeteer captcha script look like?
The file below is the complete script, in 56 lines. It defines post, solve and pause, launches the browser, captures the element, submits it as an aws-waf task and clicks the returned points. The shorter block under it gives the lines to add for a slider. Replace YOUR_API_KEY, YOUR_API_URL, PAGE_URL and the selectors with your own values.
solve_puppeteer.js · Complete Node.js client: one createTask call, then getTaskResult until the task is ready (preview format).const puppeteer = require("puppeteer");
const API_KEY = "YOUR_API_KEY";
const BASE_URL = "YOUR_API_URL";
const PAGE_URL = "https://example.com/protected";
const CAPTCHA_SELECTOR = "#captcha-container";
const POLL_MS = 1500;
const TIMEOUT_MS = 120000;
async function post(route, body) {
const response = await fetch(BASE_URL + route, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ clientKey: API_KEY, ...body }),
});
const result = await response.json();
if (result.errorId) throw new Error(`${result.errorCode}: ${result.errorDescription}`);
return result;
}
async function solve(task) {
let result = await post("/createTask", { task });
if (result.status === "ready") return result.solution;
const taskId = result.taskId;
const deadline = Date.now() + TIMEOUT_MS;
while (Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, POLL_MS));
result = await post("/getTaskResult", { taskId });
if (result.status === "ready") return result.solution;
}
throw new Error(`task ${taskId} not ready after ${TIMEOUT_MS / 1000} s`);
}
const pause = () => new Promise((resolve) => setTimeout(resolve, 50 + Math.random() * 100));
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(PAGE_URL);
const el = await page.waitForSelector(CAPTCHA_SELECTOR, { visible: true });
const png = Buffer.from(await el.screenshot());
const box = await el.boundingBox();
const scale = png.readUInt32BE(16) / box.width;
const image = png.toString("base64");
const solution = await solve({ type: "aws-waf", image });
for (const [x, y] of solution.points) {
await page.mouse.move(box.x + x / scale, box.y + y / scale, { steps: 20 });
await pause();
await page.mouse.down();
await pause();
await page.mouse.up();
await pause();
}
console.log(solution);
await browser.close();
})();
solve_puppeteer.js · Lines to add to the Node.js example for a slider: same solve function, the handle is dragged over the returned distance.const SLIDER_SELECTOR = "#slider-captcha";
const HANDLE_SELECTOR = "#slider-handle";
const slider = await page.waitForSelector(SLIDER_SELECTOR, { visible: true });
const sliderImage = Buffer.from(await slider.screenshot()).toString("base64");
const distance = (await solve({ type: "binance", image: sliderImage })).offset / scale;
const handleBox = await (await page.$(HANDLE_SELECTOR)).boundingBox();
const startX = handleBox.x + handleBox.width / 2;
const startY = handleBox.y + handleBox.height / 2;
const steps = 10 + Math.floor(Math.random() * 6);
await page.mouse.move(startX, startY, { steps: 10 });
await page.mouse.down();
for (let i = 1; i <= steps; i++) {
const jitter = i === steps ? 0 : Math.random() * 4 - 2;
await page.mouse.move(startX + (distance * i) / steps, startY + jitter);
await pause();
}
await page.mouse.up();
Captcha in Puppeteer: frequently asked questions
Does Puppeteer include a captcha solver?
No. Puppeteer controls Chrome: it loads pages, captures elements and sends mouse events, but it does not interpret a picture. The example adds the missing part with two calls to a solver API, createTask then getTaskResult, made with the fetch function built into Node.js 18. The script needs no package besides puppeteer itself, and the solver replies with points to click or with an offset to drag.
Why does the example wrap the screenshot in Buffer.from?
Because the type returned by the screenshot method depends on the Puppeteer version: recent versions return a Uint8Array, older ones a Buffer. Buffer.from accepts both and gives the two methods the example relies on: readUInt32BE, to read the width of the image in the PNG header, and toString with base64, to encode the image for the task. Without it, the script would fail on one of the two families of versions.
When is the scale different from 1 in Puppeteer?
By default Puppeteer opens pages with a viewport of 800 by 600 pixels and a device scale factor of 1, so a screenshot has the same width as the element and the scale is 1. It changes as soon as you set deviceScaleFactor to 2 in the viewport, or pass defaultViewport as null on a high-density display: the PNG is then twice as wide. Computing the scale from the PNG keeps the clicks right in every case.
Can the script attach to a Chrome that is already running?
Yes. Start Chrome with a remote debugging port, then replace puppeteer.launch with puppeteer.connect and its browserURL option. The package puppeteer-core offers the same API without downloading a browser, which suits a machine where Chrome is already installed. The capture, the scale and the mouse code do not change: they only need a page object, whichever way the browser was started.
Sources
The facts and figures quoted on this page come from these sources.
- Puppeteer, ElementHandle.screenshot() method, Puppeteer API reference.
- Puppeteer, Mouse class, Puppeteer API reference (move, down, up).