Installing Node.js and npm
Node.js Installation:
- Visit the official Node.js website (Download Node.js).
- Download the recommended LTS (Long Term Support) version for your operating system (Windows, macOS, Linux).
- Run the installer and follow the installation instructions.
- Verify the installation by opening a terminal (command prompt on Windows) and running:
node --version
npm --version
This will display the installed Node.js and npm versions.
Installing the library via npm:
First, Create the application using the following command:
npm init -y
Install the required dependencies using the following command:
npm install puppeteer
The Updated dependencies in your package.json file is:
"dependencies":{
"puppeteer": "^22.8.1",
}
Once installed, puppeteer can be initialized within a Node.js script to launch a browser instance and perform various operations.
Example: Here’s a simple Puppeteer script demonstrating browser automation:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Navigate to the URL you want to screenshot
console.log("Navigating to the webpage...");
await page.goto('https://example.com');
console.log("Page loaded successfully.");
// Take a screenshot and save it as 'example.png' in the current directory
console.log("Taking screenshot...");
await page.screenshot({ path: 'example.png' });
console.log("Screenshot saved as 'example.png'.");
await browser.close();
})();
Output:
NPM Puppeteer
Puppeteer, a Node.js library, offers a robust high-level API for controlling Chrome/Chromium browsers through the DevTools Protocol. It excels in automating browser tasks and interactions, making it a go-to choice for developers seeking efficient automation solutions.
Puppeteer operates seamlessly with Chrome/Chromium, providing developers with the ability to manage browser instances programmatically. It can run in headless mode by default, allowing automated tasks without a graphical user interface. Alternatively, it can be configured to operate in full (“headful“) mode for debugging and interactive purposes.
Contact Us