Skip to main content

HOW TO AUTOMATE CLIPPED SCREENSHOTS USING NODE JS AND PUPPETEER


Hi, welcome back to another tutorial on puppeteer and Node js. We have been on a series of tutorials concerning this powerful library. If you are totally new here, I will recommend you check out our recent tutorials on this subject and equally feel the magic of Google puppeteer. Puppeteer is real an awesome API, very useful in many ways.

Today, we will discuss how to perform screen capture of a particular area on the screen. Just like anyone would want to use snipping tool app to capture a particular area on the screen. As we already know, Puppeteer is headless chrome. Therefore, it behaves the way every user in front of a browser would do. To capture a particular portion of the screen, you need to;

  • Open a browser,
  • Create new tab or page,
  • Type in a web address or url,
  • capture the particular section you wish to,
  • and save in a directory.
  • Finally, close the browser (perhaps)

Puppeteer makes this possible without the need to open a physical browser window. Isn’t it glorious!!?

In our last article, we saw to capture screenshots and save in a directory. The exercise today is similar in code but except for the fact that we will have to set screen coordinates where we would want to capture..

Before diving to the code, have you ever thought of how useful this could be?
Perhaps to developers and non-developers?... or how applicable is this to solving a problem ?... Just a food for thought..Drop comments in the comment section.

...

Puppeteer uses the asynchronous function along side with specific methods to accomplish the steps mentioned above.

The following code has comments beside which explains each line. You should note that running such script needs to use a working internet connection as it requires opening a URL or website.

server.js

const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch() //lauch browser
const page = await browser.newPage() //new page ...similar to a tab
// set the viewport so we know the dimensions of the screen
await page.setViewport({ width: 1280, height: 800 }) // page size
await page.goto('https://www.trycoder.com/'); //open a link
await page.screenshot({ //capture screen at given coordinates (clip)
path: './uploads/image.png',
fullPage: false,
clip: {
x: 0,
y: 300,
width: 1000, // captured image size
height: 100
}
})
await browser.close() //close browser
console.log ('done');
})()

Ensure you have the uploads folder in your project directory. 

Notice the extension put after the image name.

Output

After running the script, if everything goes well, it’ll log “done” on the console and then you will find the image in your directory.

HOW TO AUTOMATE CLIPPED SCREENSHOTS USING NODE JS AND PUPPETEER

And that’s it guys, we got the expected output.

To know more about the puppeteer methods, check the API documentation at: https://developers.google.com/web/tools/puppeteer

Comments

Popular posts from this blog

JavaScript Functions

  Hi there, and welcome to another exciting lesson on JavaScript. If this is your first time here, please do well to check out our previous lessons. In the previous lesson, we were discussing JavaScript math methods. It was very intuitive as we got to learn how to use the various math methods. To proceed, we shall be looking at JavaScript functions.  A function in JavaScript like in every other programming language could be defined as a block of code that is written to perform a particular task, and this function is usually invoked or called before it is being implemented. We have been talking about methods throughout the previous lessons right. Do you know that those methods are actually functions? Yes they are functions. You can now have an overview of the importance of functions in every programming language. How do we create a function in JS? To create a function, you follow the format function functionName(Argument) {//Block of code }. Some functions do have a return val...

JavaScript Math Methods

Hello everyone, and welcome to another exciting JavaScript lesson. In the last lessons you have been seeing other methods being used in JS (for example, the string methods). We shall go further into exploring other methods. This time, it’s going to be math methods. Do not move an inch because it’s going to be a very exciting. Before we look at what a math method is, let’s have an overview of math objects. A math object in JavaScript is a static built-in object that includes properties and methods used in performing mathematical tasks. Talking about math properties, they have the syntax Math.property . Some examples are Math.E that returns Euler’s number, Math.PI that returns PI, Math.LN2 that returns the natural logarithm of 2, and many others. The various JavaScript methods contained in the math object, thus, make mathematical operations easier and reduce effort as well as time in math-oriented programming. Some JS methods include abs( ), ceil( ), cos( ), sqrt( ), pow( ), log( ) ...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...