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 => {
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');
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();
});
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();
});
We can close the tab opened using the close() function present in puppeteer.
await page.close()
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();
});
title() command is used to get the title of the current page.
await page.title()
url() command is used to get the current URL of the browser.
await page.url()
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();
});
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.
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(); });