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

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

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...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...