Screenshot of a web page using Puppeteer with a specific browser width, 웹페이지 스크린샷

To get a screenshot of a web page using Puppeteer with a specific browser width, you can follow these steps:

  1. Install Puppeteer by running the command npm install puppeteer in your project directory.

  2. Require Puppeteer in your Node.js script:

const puppeteer = require('puppeteer');
  1. Launch a new browser instance with the desired viewport size:
const browser = await puppeteer.launch({
  defaultViewport: {
    width: 1280,
    height: 720,
  },
});
  1. Create a new page in the browser:
const page = await browser.newPage();
  1. Navigate to the desired URL:
await page.goto('https://example.com');
  1. Take a screenshot of the page:
await page.screenshot({ path: 'screenshot.png' });
  1. 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.

댓글

이 블로그의 인기 게시물

To switch to a specific tag in a Git repository

How to checkout branch of remote git, 깃 리모트 브랜치 체크아웃

Using the MinIO API via curl

To download a file from MinIO using Spring Boot, 스프링부트 Minio 사용하기

리눅스의 부팅과정 (프로세스, 서비스 관리)

Chromium 개발 환경 세팅, 크로미움 개발 준비하기

Joining an additional control plane node to an existing Kubernetes cluster

urllib3 with proxy settings

CDPEvents in puppeteer

Avro + Grpc in python