Skip to main content

TAKE SCREENSHOT OF PAGE WITH NODEJS

Have you ever wanted to take screenshot of a page using NodeJs? Then this is the right place for you to come.
Taking screenshots of webpages might come handy to you in various places of development. You might want to take the screenshot of a webpage at a particular time and you want to write a Node script for it or maybe you want to use it as a feature in your web application. Or maybe you want to screenshot the webpage of your crush on instagram when they upload a new picture? Who am I to judge ?
I remember the times when this task used to be really difficult and require us to write many lines of code and maybe use Canvas or many other things. But in this article, we’ll be doing it very easily and fast using a great npm package.
You can take the screenshot of a page using NodeJS with the puppeteer library.

Puppeteer

First, let me just give you a brief overview of the puppeteer library in case you have never used or heard of this library.
Puppeteer is an amazing library developed by the fine folks at Google that allows you to totally control your browser using NodeJS. It was originally written with Chrome and Chromium and I still use it with them only but I guess even Firefox is supported now.
Okay, so the most important use of Puppeteer could be testing but I’ll just jot some of the uses down that they themselves advertise :-
  • Taking screenshots and creating PDFs
  • Crawl websites and single page applications easily
  • Automate form submission, interact with the UI and everything related to UI testing
  • Capture timeline trace of your website
The feature we are most interested in right now is the first one, the ability to take screenshots using puppeteer fairly easily.

Installing puppeteer

Puppeteer is packed with a whole plethora of features but is still an NPM package at the end of the day. So, just like any other package you can easily install it in your project using :-
1
npm i puppeteer
Note that this would also download Chromium along that would automatically support puppeteer connection with it and I highly recommend you to use this command to install puppeteer. Still, if you do not want to install the browser along with it, you can use this command instead :-
1
npm i puppeteer-core
For more information related to puppeteer vs puppeteer core you can visit this link -> https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#puppeteer-vs-puppeteer-core
Note that this has installed puppeteer just inside your project and not globally.

Using puppeteer

Create a new file called index.js and write the following code into it to start the browser with puppeteer :-
Take screenshot of page in NodeJS code
Yes, just these many lines of code!
Let us see what these lines do one by one. First of, we just import puppeteer into our file here. Next, we’ve written an asynchronous anonymous function. If you do not know what anonymous function is, it is nothing but a function that has no name. This function here infact is called an IIFE, immediately invoked function expression because we call it immediately as it gets written.
Inside this IIFE, the first line of code launches the browser. Please note that every action puppeteer does has to be pre-pended with the await keyword as all theses actions are asynchronous in nature. So, we open the browser by passing in an option of headless: false, what this will do is actually open the browser when you run this file. You can pass headless:true for the browser not to open during the execution.
In the second line we create a new browser page. In the next line we travel to a certain URL of your choice, I’ve added a youtube link here for my file. The waitUntil parameter that is passed along is actually for waiting until some event happens on the webpage. We have used the value of networkIdle2, so as to wait until at most 2 requests are pending on that page. Note -> Do not use networkIdle0 because in most cases those are open always for websockets or something.
Now, you’re basically on the page that gets loaded on that URL you entered. Taking the screenshot is the easiest part of the code as you only need to call the method screenshot with an argument telling where and what name should the screenshot be saved as.
At the end of the function, we just use the close method to close our browser. Go to your directory where you saved the screenshot and voila! You can see your page’s screenshot there. Yes, as easy as that!

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...