DEV Community

Cover image for How to automate an MTcaptcha solution in JavaScript (Puppeteer)
Dzmitry
Dzmitry

Posted on

How to automate an MTcaptcha solution in JavaScript (Puppeteer)

Example MTCaptcha

In this example, you can see how automate an MTcaptcha solution in JavaScript using Puppeteer and the 2captcha service. Puppeteer is Node.js library using for automation. 2captcha is service used to solve the captcha.

To interact with the 2captcha API, this demo uses the 2captcha-ts package.

The entire process of automating the MTCaptcha bypass is described as comments in the index.js file:

// https://github.com/dzmitry-duboyski/MTCaptcha-solving-example/
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.pages();
  await page.setViewport({ width: 1080, height: 1024 });

  // Open target page that contains MTCaptcha.
  await page.goto("https://2captcha.com/demo/mtcaptcha");
  await page.waitForSelector("input[name='mtcaptcha-verifiedtoken']");

  // Get the `sitekey` parameter MTCaptcha from the current page.
  const mtCaptchaKey = await page.evaluate(() => {
    const mtCaptchaKey = window.mtcaptcha.getConfiguration().sitekey;
    // Or
    // const mtCaptchaKey = window.mtcaptchaConfig.sitekey
    return mtCaptchaKey;
  });

  console.log("MTCaptchaKey: " + mtCaptchaKey);

  // Show the block for clarity (this is not necessary).
  await page.evaluate(() => {
    document.querySelector("input[name='mtcaptcha-verifiedtoken']").type = "";
  });

  // Send a captcha to the 2captcha service to get a solution.
  // Solving the captcha will take some time, you need to wait.
  // If you get error 'ERROR_CAPTCHA_UNSOLVABLE' try again.
  // If necessary, you can use a proxy server to solve MTcaptcha.
  const res = await solver.mtCaptcha({
    pageurl: "https://2captcha.com/demo/mtcaptcha",
    sitekey: mtCaptchaKey,
    // proxy: 'login:password@123.123.123.123:3128',
    // proxytype: 'http'
  });

  console.log("Answer:");
  console.log(res);

  // Answer. The answer is a token that needs to be applied to the page.
  const captchaAnswer = res.data;

  // Use the resulting solution(token) on the page
  const setAnswer = await page.evaluate((captchaAnswer) => {
    document.querySelector("input[name='mtcaptcha-verifiedtoken']").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!!!");
  }

  browser.close();
})();
Enter fullscreen mode Exit fullscreen mode

In the example, a page with a captcha MTcaptcha is opened, then the sitekey value is searched on the page, then the captcha is sent to the 2captcha service for solution. When a solution is received, the resulting solution is applied on the page, and then a check occurs to see if the solution worked successfully.

If necessary, you can use a proxy server to solve MTcaptcha.

Source code: Source code

Top comments (0)