Skip to content
the poor coder

pupeteer disable script call

Poor Man
Published
Reading time
2 min read
On this page

Puppeteer Disable Script Call#

As a web developer, I have encountered situations where I needed to disable script calls in Puppeteer. Puppeteer is a Node library that allows us to control headless Chrome or Chromium. It provides a high-level API to manipulate the browser's behavior and automate web page interactions. Puppeteer is a powerful tool that can be used for web scraping, testing, and website monitoring.

Why Disable Script Calls in Puppeteer?#

There may be several reasons why you would want to disable script calls in Puppeteer. One common use case is web scraping. When we scrape a website, we may not want the website's JavaScript code to execute. This is because the JavaScript code may generate dynamic content, such as pop-ups, overlays, or alerts, that can interfere with our scraping process.

How to Disable Script Calls in Puppeteer#

The easiest way to disable script calls in Puppeteer is to set the javascriptEnabled option to false. This option disables the execution of JavaScript code on the webpage.

JavaScript

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setJavaScriptEnabled(false);
  await page.goto('https://www.example.com/');
  // Do web scraping here
  await browser.close();
})();

In this code snippet, we first launch a new instance of Puppeteer and create a new page. We then disable JavaScript by calling the setJavaScriptEnabled(false) method on the page object. We finally navigate to the website we want to scrape and do our scraping process.

Another way to disable script calls in Puppeteer is to use the requestWillBeSent event to intercept and block any JavaScript requests made by the page. This method is more advanced and requires knowledge of the Chrome DevTools Protocol. Here is an example:

JavaScript

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setRequestInterception(true);
  page.on('request', (req) => {
    if (req.resourceType() === 'script') {
      req.abort();
    } else {
      req.continue();
    }
  });
  await page.goto('https://www.example.com/');
  // Do web scraping here
  await browser.close();
})();

In this code snippet, we first enable request interception by calling the setRequestInterception(true) method on the page object. We then listen for the request event and check if the request is for a JavaScript file by calling the resourceType() method on the request object. If it is a JavaScript file, we abort the request by calling the abort() method. Otherwise, we continue the request by calling the continue() method. We finally navigate to the website we want to scrape and do our scraping process.

Conclusion#

Disabling script calls in Puppeteer can be useful for web scraping, testing, or website monitoring. We can use the javascriptEnabled option or the requestWillBeSent event to disable JavaScript execution on a webpage. It is important to choose the method that best suits our use case and to test our code thoroughly to ensure it works as expected.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC