-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (50 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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();
await page.setViewport({ width: 1080, height: 1024 });
// Open target page
await page.goto("https://2captcha.com/demo/hcaptcha?difficulty=difficult");
await page.waitForSelector("div.h-captcha iframe");
// Get the `sitekey` parameter from the current page
const sitekey = await page.evaluate(() => {
const url = document.querySelector("div.h-captcha iframe").src;
const pureSiteKey = url.split("sitekey=")[1].split("&")[0];
return pureSiteKey;
});
console.log(`sitekey: ${sitekey}`);
// Send a captcha to the 2captcha service to get a solution
const res = await solver.hcaptcha({
pageurl: "https://2captcha.com/demo/hcaptcha?difficulty=difficult",
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(
"textarea[name='h-captcha-response']"
).style.display = "block";
document.querySelector("textarea[name='h-captcha-response']").value =
captchaAnswer;
}, captchaAnswer);
// Press the button to check the result.
await page.click('button[type="submit"]');
// Check result
await page.waitForSelector("form div pre code");
const resultBlockSelector = "form div pre code";
let statusSolving = await page.evaluate((selector) => {
return document.querySelector(selector).innerText;
}, resultBlockSelector);
statusSolving = JSON.parse(statusSolving);
if (statusSolving.success) {
console.log("Captcha solved successfully!!!");
}
await page.waitForTimeout(5000);
browser.close();
})();