Naughty with Puppeteer (P1)

Tram Ho

1. What is Pupperteer?

Original

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.

Provisional translation is:

Puppeteer is a Node library that provides high-level APIs to control Chrome or Chromium using the DevTools protocol. Puppeteer by default runs headless , but can be configured to run non-headless.

I will explain a little: As I understand, run headless is that you run the browser without the interface. Running a browser like that is usually for crawling data, taking screenshots, and not browsing.

Some nice things you can do with Pupperteer are:

  • Take screenshots or export pdf files of pages.
  • Crawl a SPA (Single-Page Application) and output pre-rendered content (eg “SSR” (Server-Side Rendering)).
  • Automatically submit form, test interface and input data from keyboard, …
  • Create an automated testing environment. Run the beta in Chorme with the latest features and latest javascript versions.
  • Recording timeline traces for your website helps to detect performance issues early.
  • Test Chorme Extensions.

In this article, I will try the puppeteer with you to take a screenshot or export a pdf file ? Other features, we’ll see you in the future ?

2. It’s naughty

First, I will init a Node project in the test folder

Do not forget to install the puppeteer for the project:

Now I will write the index.js file to play around with the ahihi ? .

2.1 Take a screenshot

Type run command and wait for the result:

Result:

 

2.2 Try exporting a pdf file

Result:

2.3 Capture page of authenticate request

What if you take a page that requires you to log in?

For example, here, I want to take a screenshot of the writing page of the Viblo page.

Result:

As in this case, we will capture the login page. Because when the brower run and access to https://viblo.asia/publish/post , the router is redirected to the login page because the page we want to access requires authenticate.

The solution is that we will have to consider a cookie before visiting that site. Try it again:

The result you get will look like this

I wish you success and see you in the next article.

References:

https://developers.google.com/web/tools/puppeteer/

Share the news now

Source : Viblo