This example shows how to automate the solution of Yandex SmartCaptcha on the demo captcha page https://captcha-api.yandex.ru/demo. For automation we use the library Puppeteer and the captcha solving service 2captcha.com. For correct work of the example you need APIKEY
, for this you need to have an account in the service 2captcha.com, APIKEY
is displayed in your personal cabinet.
git clone https://github.com/dzmitry-duboyski/solving-yandex-smart-captcha-using-puppeteer.git
npm install
Set up you APIKEY
in ./index.js#L3
APIKEY
is specified in your 2captcha.com account. Before copyingAPIKEY
, make sure that the "developer" role is selected in your account.
npm run start
import puppeteer from "puppeteer";
import { Solver } from "2captcha-ts";
const solver = new Solver("<Your 2captcha APIKEY>");
;(async () => {
const browser = await puppeteer.launch({
headless: false,
});
const page = await browser.newPage();
// Open target page
await page.goto("https://captcha-api.yandex.ru/demo");
await page.waitForSelector("#captcha-container");
await page.waitForSelector("iframe[data-testid='checkbox-iframe'");
// Enter data
await page.$eval("#name", (el) => (el.value = ""));
await page.type("#name", "John Doe", { delay: 100 });
// Get the `sitekey` parameter from the current page
const sitekey = await page.evaluate(() => {
return document
.querySelector("#captcha-container")
.getAttribute("data-sitekey");
});
// Send a captcha to the 2captcha service to get a solution
const res = await solver.yandexSmart({
pageurl: "https://captcha-api.yandex.ru/demo",
sitekey: sitekey,
});
console.log(res);
// The resulting solution
const captchaAnswer = res.data;
// Use the resulting solution on the page
const setAnswer = await page.evaluate((captchaAnswer) => {
document.querySelector("input[data-testid='smart-token']").value =
captchaAnswer;
}, captchaAnswer);
// Click on the 'Submit' button to check the captcha solution
await page.click("#smartcaptcha-demo-submit");
await page.waitForSelector(".greeting");
console.log("Captcha solved successfully!!!");
browser.close();
})();
Source code index.js
Important
If you need to solve the Yandex captcha that needs to be clicked, it is recommended to use Coordinates API v1 \ Coordinates API v2 method, more details are described in article.