-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (47 loc) · 1.79 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
import puppeteer from "puppeteer";
const APIKEY = process.env.APIKEY;
import { Solver } from "2captcha-ts";
const solver = new Solver(APIKEY);
(async () => {
const browser = await puppeteer.launch({
headless: false,
});
const page = await browser.newPage();
await page.setViewport({ width: 1080, height: 1024 });
await page.goto("https://2captcha.com/demo/recaptcha-v2");
await page.waitForSelector(".g-recaptcha");
// Extract the `sitekey` parameter from the page.
const sitekey = await page.evaluate(() => {
return document.querySelector(".g-recaptcha").getAttribute("data-sitekey");
});
// Get actual page url
const pageurl = await page.url();
// Submitting the captcha for solution to the service
const res = await solver.recaptcha({
pageurl: pageurl,
googlekey: sitekey,
});
console.log(res);
// Getting a captcha response including a captcha answer
const captchaAnswer = res.data;
// Use captcha answer
const setAnswer = await page.evaluate((captchaAnswer) => {
// It is not necessary to make this block visible, it is done here for clarity.
document.querySelector("#g-recaptcha-response").style.display = "block";
document.querySelector("#g-recaptcha-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();
})();