How to Bypass CAPTCHA With Puppeteer

To access protected websites, you must bypass CAPTCHA. Puppeteer, a Node.js library with a user-friendly API for managing Chrome/Chromium via the DevTools Protocol, can help. It can run in full-browser mode instead of headless mode.

Well, why isn’t Puppeteer enough? Automated access using Puppeteer often triggers CAPTCHA or blocks as websites detect the automation.

Let’s validate it using the following steps:

1. You must have Node.JS installed on your system.

Create a new Node.JS project and install Puppeteer using the following npm command:

npm i puppeteer

2. Import the Puppeteer library in your Node.JS file.

const puppeteer = require('puppeteer');

3. Create a new browser instance in headless mode and a new page using the following code:

(async () => {
  // Create a browser instance
  const browserObj = await puppeteer.launch();

  // Create a new page
  const newpage = await browserObj.newPage();

4. Since we need to take the screenshot on the desktop device, we can set the viewport size using the following code:

  // Set the width and height of viewport
  await newpage.setViewport({ width: 1920, height: 1080 });

The setViewPort() method sets the size of the webpage. You can change it according to your device requirements.

5. After that, navigate to a page URL (that you think is a CAPTCHA-protected page) and take a screenshot.

For demonstration purposes, the code uses Oxylabs scraping sandboxarrow-up-right. Remember to close the browser object at the end.

This is what the complete code looks like:

Using Puppeteer-stealth to bypass CAPTCHA

Here is the step-by-step procedure to implement this CAPTCHA bypass:

1. To start, you need to install the puppeteer-extra and puppeteer-extra-plugin-stealth packages.

2. After that, import the following required libraries in your Node.JS file:

3. The next step is to create the browser object in headless mode, navigate to the URL and take a screenshot.

The setUserAgent method makes our requests imitate a real browser's User-Agent, making our automated headless browsers appear more like regular users. Setting one of the common User-Agent strings helps evade detection and bypass anti-bot mechanisms that analyze the User-Agent header.

Here is what our complete script looks like:

Using Web Unblocker with Node.JS

Web Unblocker uses AI to help users prevent CAPTCHA and gain access to public data from websites with advanced anti-bots implemented. To begin, you can send a basic query without any special options – the Web Unblocker tool will select the fastest CAPTCHA proxy, add all necessary headers, and provide you with the response body.

1. Install the node-fetch and HttpsProxyAgent using the following command:

npm install node-fetch https-proxy-agent

2. Sign up to Oxylabsarrow-up-right and get your credentials for using the API.

3. Before importing the libraries, open the package.json file and enter these lines "type": "module", for example:

Since the newest version of node-fetch is an ESM-only module, you can’t import it using the require() function. Learn more about it herearrow-up-right.

Next, import the required modules in your JS file using the import-from syntax:

The fs library can help save the response in an HTML file.

4. Provide your user credentials and set up a proxy using HttpsProxyAgent.

5. Next, set the URL and issue a fetch request.

The environment variable NODE_TLS_REJECT_UNAUTHORIZED is set to zero so that Node.JS doesn't verify the SSL/TLS certificates. This is a required setting if you’re using Oxylabs’ Web Unblocker.

6. In the end, you can convert the response into text and save it in an HTML file.

Here is the complete script:

Last updated

Was this helpful?