-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
74 lines (62 loc) · 2.08 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
65
66
67
68
69
70
71
72
73
74
require("chromedriver");
require("dotenv").config();
const Client = require("@infosimples/node_two_captcha");
const { Builder, By, Key, until } = require("selenium-webdriver");
const client = new Client(process.env.CAPTCHA_API_KEY, {
timeout: 60000,
polling: 5000,
throwErrors: false,
});
const initiateCaptchaRequest = async () => {
console.log("solving captcha...");
try {
client
.decodeRecaptchaV2({
googlekey: process.env.GOOGLE_CAPTCHA_KEY,
pageurl: process.env.WEBSITE_URL,
})
.then(function (response) {
// if captcha is solved, launch selenium driver.
launchSelenium(response);
});
} finally {
// do something
}
};
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function launchSelenium(response) {
if (response) {
console.log("Captcha Solved! Launching Browser instance...");
let driver = await new Builder().forBrowser("chrome").build();
// Navigate to Url
await driver.get(process.env.WEBSITE_URL);
await driver.findElement(By.id("name")).sendKeys("Ted");
await driver.findElement(By.id("phone")).sendKeys("000000000");
await driver.findElement(By.id("email")).sendKeys("tngeene@captcha.com");
await driver.findElement(By.id("comment-content")).sendKeys("test comment");
const gCaptchResponseInput = await driver.findElement(
By.id("g-recaptcha-response")
);
await driver.executeScript(
"arguments[0].setAttribute('style','type: text; visibility:visible;');",
gCaptchResponseInput
);
await gCaptchResponseInput.sendKeys(`${response.text}`);
await driver.executeScript(
"arguments[0].setAttribute('style','display:none;');",
gCaptchResponseInput
);
await driver.findElement(By.id("send-message")).click();
// wait 8 seconds and close browser window
await sleep(8000);
driver.quit();
} else {
// if no text return request time out message
console.log("Request timed out.");
}
}
(async function main() {
const response = await initiateCaptchaRequest();
})();