To get a screenshot of a web page using Puppeteer with a specific browser width, you can follow these steps:
- Install Puppeteer by running the command
npm install puppeteer
in your project directory. - Require Puppeteer in your Node.js script:
const puppeteer = require('puppeteer');
- Launch a new browser instance with the desired viewport size:
const browser = await puppeteer.launch({
defaultViewport: {
width: 1280,
height: 720,
},
});
- Create a new page in the browser:
const page = await browser.newPage();
- Navigate to the desired URL:
await page.goto('https://example.com');
- Take a screenshot of the page:
await page.screenshot({ path: 'screenshot.png' });
- Close the browser instance:
await browser.close();
Here's the complete code:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
defaultViewport: {
width: 1280,
height: 720,
},
});
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'screenshot.png' });
await browser.close();
})();
This code will save a screenshot of the web page with a width of 1280 pixels and a height of 720 pixels as a PNG image file named screenshot.png
in the current directory. You can modify the viewport size and the path/filename to your liking.