Open Browser with puppeteer

launch() function opens a browser with few attributes like 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 by 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 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();

});

Open new tab

You open new tab in puppeteer using newPage() metod present in browser object.

const page = await browser.newPage();

Complete code for opening the new tab in the browser.

const page = await browser.newPage();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

Close tab

We can cloes the tab openied 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();
});

Get title

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

await page.title()

Get url of curent tab in puppeteer

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

await page.url()

Content of page / Page Source

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

await page.content()

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();
});

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