Open Browser with puppeteer

launch() function opens a browser with few attributes like the size of the browser and whether to be headless or not and to set few more attributes.

puppeteer.launch({headless:false}).then(async browser => {

Open url in Puppeteer

The goto() method in puppeteer is used to navigate to a web page, passing the string URL as a parameter.

await page.goto('https://chercher.tech');

Close browser

The browser.close() command is used to close the browser in the puppeteer.

await browser.close();

The complete code for closing the browser.

const puppeteer = require('puppeteer');
puppeteer.launch({headless:false}).then(async browser => {
  const page = await browser.newPage();
  await page.goto('https://chercher.tech');
  await browser.close();
});

Working with tabs

Open new tab

You open a new tab in puppeteer using the newPage() method present in the browser object.

const page = await browser.newPage();

Complete code for opening the new tab in the browser.

const puppeteer = require('puppeteer');
puppeteer.launch({headless:false}).then(async browser => {
  const page = await browser.newPage();
  await page.goto('https://chercher.tech');
  await browser.close();
});

Close tab in Puppeteer

We can close the tab opened using the close() function present in puppeteer.

await page.close()

Close browser:

We can also close all the tabs present in the browser, using close() method from the browser.

await browser.close();

Complete code to close the tab and browser.

const puppeteer = require('puppeteer');
puppeteer.launch({headless: false}).then(async browser => {
	const page = await browser.newPage();
	await page.goto('https://chercher.tech');
	await page.close() await browser.close();
});

Page Commands in puppeteer

get Title of a page

title() command is used to get the title of the current page.

await page.title()

Get url of current tab in puppeteer

url() command is used to get the current URL of the browser.

await page.url()

Content of page / Page Source

content() command is used to get the source code of the page.

await page.content()

The complete code for puppeteer browser commands

const puppeteer = require('puppeteer');
puppeteer.launch({headless: false}).then(async browser => {
	const page = await browser.newPage();
	await page.goto('https://chercher.tech');
	console.log("title : " + await page.title()) 
	console.log("url : " + await page.url()) 
	console.log("Page Source : " + await page.content()) 
	await page.close() await browser.close();
});

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • const puppeteer = require('puppeteer');
    puppeteer.launch({headless:false}).then(async browser => {
      const page = await browser.newPage();
      await page.goto('https://chercher.tech');
      await browser.close();
    });
    Reply