How to handle tabs in Puppeteer?


We can handle tabs in Puppeteer using the below methods −

  • newPage() - We can open a new tab using this method available in the browser object.

Syntax

const p = await browser.newPage()
  • close() - We can close the tab opened using this method.

Syntax

await p.close()
  • close() - We can close all the tabs opened using this method available in the browser object.

Syntax

await browser.close()

Example

Code Implementation

//adding Puppeteer library
const pt = require('puppeteer')
pt.launch().then(async browser => {
   //browser new page
   const p = await browser.newPage();
   //set viewpoint of browser page
   await p.setViewport({ width: 1000, height: 500 })
   //launch URL
   await p.goto('https://www.tutorialspoint.com/index.htm')
   //capture screenshot
   await p.screenshot({
   path: 'tutorialspoint.png'
});
//browser close
await browser.close()

Output

Updated on: 19-Nov-2021

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements