Disable JavaScript in Puppeteer

Disabling JavaScript will take up less CPU and RAM on your computer, which is to be expected. If you run something super basic, it’ll take up fewer resources.

Sometimes you might need to verify the functionalities of the websites when there is no javascript present. Also, this will help in some browser where the javascript is disabled.

We can disable the javascript by not downloading the javascript components to the local system from the remote machine.

Most of the websites use the javascript, you can view it by opening the dev tool of chrome or by pressing Ctrl + Shift + I. In the below screenshot, you can see few Javascript files downloaded while opening google.com.

disable-javascript-puppeteer

Disable Javascript in Puppeteer:

To disable javascript, we need to monitor all the requests/responses flowing. Then based on the type, we can decide to terminate then request/response.

In the below example, we will load flipkart.com without using the javascript files.

const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setRequestInterception(true);
  page.on('request', request => {
    if (request.resourceType() === 'script')
      request.abort();
    else
      request.continue();
  });
  await page.goto('https://www.flipkart.com');
  await browser.close();
  })();
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Protractor Training