English 中文(简体)
Puppeteer - Disable JavaScript
  • 时间:2024-12-22

Puppeteer - Disable JavaScript


Previous Page Next Page  

We can disable JavaScript using Puppeteer. For this, we have to block the requests/response based on its type. Let us make an attempt to launch a page by disabpng JavaScript.

To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows −

Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed).

The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation.

Right-cpck on the folder where the node_modules folder is created, then cpck on the New file button.

Node Modules

Step 2 − Enter a filename, say testcase1.js.

Testcase1.JS

Step 3 − Add the below code within the testcase1.js file created.


//Puppeteer pbrary
const pt = require( puppeteer )
pt.launch().then(async browser => {
//browser new page
   const page = await browser.newPage()
   //monitor requests
   await page.setRequestInterception(true)
   //check resourceType is script
   page.on( request , request => {
      if (request.resourceType() ===  script )
         request.abort();
      else
         request.continue();
   })
   //launch apppcation
   await page.goto( https://www.tutorialspoint.com/index.htm )
   //browser close
   await browser.close()
})

Step 4 − Execute the code with the command given below −


node <filename>

So in our example, we shall run the following command −


node testcase1.js
Advertisements