How to Solve ReCaptcha v3 in JavaScript using Selenium & CapMonster Cloud
- To run the code, you need to install Node.js and all the necessary dependencies (see JavaScript ReCaptcha v.2). In the “.env” file we write the values:
CAPMONSTER_API_KEY=YOUR_API_KEY
WEBSITE_KEY=6Le0xVgUAAAAAIt20XEB4rVhYOODgTl00d8juDob
WEBSITE_URL=https://lessons.zennolab.com/captchas/recaptcha/v3.php?level=beta
- In the “main.js” file we write code to automatically solve this type of captcha. We need to know the id in advance to search for the captcha element in order to return the token received from the CapMonster Cloud server there.
require('dotenv').config();
const { Builder, By, until } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const { CapMonsterCloudClientFactory, ClientOptions, RecaptchaV3ProxylessRequest } = require('@zennolab_com/capmonstercloud-client');
const CAPMONSTER_API_KEY = process.env.CAPMONSTER_API_KEY;
const WEBSITE_KEY = process.env.WEBSITE_KEY;
const WEBSITE_URL = process.env.WEBSITE_URL;
async function solveRecaptcha() {
const options = new chrome.Options();
const driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
// Initializing the CapMonster client
const cmcClient = CapMonsterCloudClientFactory.Create(new ClientOptions({ clientKey: CAPMONSTER_API_KEY }));
try {
await driver.get(WEBSITE_URL);
console.log('Browser initialized!');
const recaptchaV3ProxylessRequest = new RecaptchaV3ProxylessRequest({
websiteURL: WEBSITE_URL,
websiteKey: WEBSITE_KEY,
minScore: 0.7,
pageAction: "verify",
});
// Getting a captcha solution from CapMonster
const solution = await cmcClient.Solve(recaptchaV3ProxylessRequest);
console.log('Solution CapMonster:', solution);
// Inserting a token into a captcha form
await driver.executeScript((result) => {
document.getElementById('g-recaptcha-response-100000').style.display = 'block';
document.getElementById('g-recaptcha-response-100000').style.visibility = 'visible';
document.getElementById('g-recaptcha-response-100000').value = result;
}, solution);
console.log('The result is inserted into the text field!');
await Promise.all([
driver.wait(until.elementIsVisible(driver.findElement(By.id('v3_submit')))),
driver.findElement(By.id('v3_submit')).click()
]);
console.log('"Check" button pressed!');
} catch (e) {
console.error('An error has occurred:', e);
} finally {
await driver.quit();
}
}
solveRecaptcha();
Code explanation:
Loading environment variables: dotenv is used to load environment variables from the .env file such as CAPMONSTER_API_KEY, WEBSITE_KEY and WEBSITE_URL.
solveRecaptcha function:
Creates an instance of the Chrome driver using Selenium WebDriver.
Initializes the CapMonster client using the loaded CAPMONSTER_API_KEY.
Goes to a website with reCAPTCHA.
Creates a V3 Proxyless reCAPTCHA challenge with parameters such as minScore and pageAction.
Solves reCAPTCHA using CapMonster and gets a solution.
Inserts the resulting token (solution) into a form on a web page using JavaScript.
Waits for the element with id 'v3_submit' to be visible and then clicks on it.
Displays messages about completed steps.
try-catch-finally block:
The try block contains the main automation execution code.
The catch block handles any exceptions and prints an error message.
The finally block ensures that the driver will be closed even if an error occurs.
Calling solveRecaptcha() function: runs the solveRecaptcha() function to solve reCAPTCHA when the script is executed.
Running the code and the result:
Note: We'd like to remind you that the product is used to automate testing on your own websites and on websites to which you have legal access.