Cloudflare Turnstile
Here we will fill out a small contact form on the test site tsmanaged.zlsupport.com and solve a Cloudflare Turnstile captcha. As for previous types of captchas, we need to find out the sitekey of the captcha in the developer tools in the browser.
- Let's import the dependencies and write the values:
import { launch } from 'puppeteer';
import { CapMonsterCloudClientFactory, ClientOptions, TurnstileProxylessRequest } from '@zennolab_com/capmonstercloud-client';
const CAPMONSTER_API_KEY = 'YOUR_API_KEY';
const WEBSITE_KEY = 'EXAMPLE_SITE_KEY';
const WEBSITE_URL = 'www.example.com';
- Let's launch the browser, create a task for CapMonster Cloud, send it to the server, receive and insert the result into the page:
async function solveCloudflareTurnstileCaptcha() {
const browser = await launch({ headless: false });
const page = await browser.newPage();
// Initializing the CapMonster client
const cmcClient = CapMonsterCloudClientFactory.Create(new ClientOptions({ clientKey: CAPMONSTER_API_KEY }));
try {
await page.goto(WEBSITE_URL);
console.log('Page is open, solving Cloudflare Turnstile CAPTCHA...');
await page.type('#username', 'login');
await page.type('#password', 'pass');
const turnstileTaskRequest = new TurnstileProxylessRequest({
websiteURL: WEBSITE_URL,
websiteKey: WEBSITE_KEY,
});
// Getting a captcha solution from CapMonster
const solutionObject = await cmcClient.Solve(turnstileTaskRequest);
console.log('CapMonster Solution:', solutionObject);
const token = solutionObject.solution.token;
console.log('Captcha solved:', token);
// Inserting a token into the "token" field
await page.evaluate((extractedToken) => {
const tokenField = document.querySelector('#token');
if (tokenField) {
tokenField.value = extractedToken;
}
}, token);
console.log('Token inserted into the "token" field!');
// Click on the "Submit" button after inserting the token
await page.evaluate(() => {
const submitButton = document.querySelector('button[type="submit"]');
if (submitButton) {
submitButton.click();
}
});
console.log('Clicked on the "Submit" button after token insertion!');
await new Promise(resolve => setTimeout(resolve, 5000));
} catch (error) {
console.error('Error:', error);
} finally {
await browser.close();
}
}
solveCloudflareTurnstileCaptcha();
Explanation of this part of the code:
async function solveCloudflareTurnstileCaptcha() { ... }: Declaring an asynchronous function solveCloudflareTurnstileCaptcha, which contains all the code to solve the Cloudflare Turnstile CAPTCHA.
const browser = await launch({ headless: false });: Launching a browser using Puppeteer. The headless: false option specifies that the browser will be launched with a GUI.
const page = await browser.newPage();: Creating a new tab (page) in the browser.
const cmcClient = CapMonsterCloudClientFactory.Create(new ClientOptions({ clientKey: CAPMONSTER_API_KEY }));: Initializing the CapMonster client using the provided API key.
await page.goto(WEBSITE_URL);: Going to page with Cloudflare Turnstile CAPTCHA.
console.log('Page opened, solving Cloudflare Turnstile CAPTCHA...');: Displaying a message to the console indicating that the page is open and the process of solving the Cloudflare Turnstile CAPTCHA has begun.
await page.type('#username', 'login');: Data entry in the "username" field.
await page.type('#password', 'pass');: Data entry in the "password" field.
const turnstileTaskRequest = new TurnstileProxylessRequest({ ... });:Creating a request object for the Turnstile CAPTCHA solution. Set parameters such as website URL and Turnstile CAPTCHA key.
const solutionObject = await cmcClient.Solve(turnstileTaskRequest);: Sending a request to CapMonster for the Turnstile CAPTCHA solution. The result is saved in a solutionObject.
console.log('CapMonster Solution:', solutionObject);: Displaying information about the solution from CapMonster to the console.
const token = solutionObject.solution.token;: Retrieving a token (solution result) from a solutionObject.
console.log('Captcha solved:', token);: Displaying a message to the console that the CAPTCHA has been solved and displaying the token.
await page.evaluate((extractedToken) => { ... }, token);: Inserting a token into the "token" field on a web page using Puppeteer's evaluate method.
console.log('Token inserted into the "token" field!');: Displaying a message to the console indicating that a token has been inserted into the "token" field.
await page.evaluate(() => { ... });: Click on the "Submit" button after inserting the token using the Puppeteer evaluate method.
console.log('Clicked on the "Submit" button after token insertion!');: Output a message to the console indicating that a click operation was performed on the "Submit" button after inserting a token.
await new Promise(resolve => setTimeout(resolve, 5000));: Pause for 5 seconds using Promise.
} catch (error) { console.error('Error:', error); } finally { await browser.close(); }:Handling errors (if they occur) with error message output to the console. Closing the browser in a finally block to ensure completion.
solveCloudflareTurnstileCaptcha();: Calling the solveCloudflareTurnstileCaptcha function to complete the entire process of solving the Cloudflare Turnstile CAPTCHA.
Full code:
import { launch } from 'puppeteer';
import { CapMonsterCloudClientFactory, ClientOptions, TurnstileProxylessRequest } from '@zennolab_com/capmonstercloud-client';
const CAPMONSTER_API_KEY = 'YOUR_API_KEY';
const WEBSITE_KEY = 'EXAMPLE_SITE_KEY';
const WEBSITE_URL = 'www.example.com';
async function solveCloudflareTurnstileCaptcha() {
const browser = await launch({ headless: false });
const page = await browser.newPage();
// Initializing the CapMonster client
const cmcClient = CapMonsterCloudClientFactory.Create(new ClientOptions({ clientKey: CAPMONSTER_API_KEY }));
try {
await page.goto(WEBSITE_URL);
console.log('Page opened, solving Cloudflare Turnstile CAPTCHA...');
await page.type('#username', 'login');
await page.type('#password', 'pass');
const turnstileTaskRequest = new TurnstileProxylessRequest({
websiteURL: WEBSITE_URL,
websiteKey: WEBSITE_KEY,
});
// Getting a captcha solution from CapMonster
const solutionObject = await cmcClient.Solve(turnstileTaskRequest);
console.log('CapMonster Solution:', solutionObject);
const token = solutionObject.solution.token;
console.log('Captcha solved:', token);
// Inserting a token into the "token" field
await page.evaluate((extractedToken) => {
const tokenField = document.querySelector('#token');
if (tokenField) {
tokenField.value = extractedToken;
}
}, token);
console.log('Token inserted into the "token" field!');
// Click on the "Submit" button after inserting the token
await page.evaluate(() => {
const submitButton = document.querySelector('button[type="submit"]');
if (submitButton) {
submitButton.click();
}
});
console.log('Clicked on the "Submit" button after token insertion!');
await new Promise(resolve => setTimeout(resolve, 5000));
} catch (error) {
console.error('Error:', error);
} finally {
await browser.close();
}
}
solveCloudflareTurnstileCaptcha();
- Let's run the code. If everything is done correctly, the code will solve the captcha and open a confirmation page: