Skip to main content

How to perform mouse operations using node js and puppeteer

 

How to perform mouse operations using node js and puppeteer
Hi guys, welcome to another tutorial where we are going to discuss another magic using google puppeteer. In our last tutorial, we talked about how to emulate mobile devices and we saw how fascinating and useful it is. Today, we are going to see how we can perform user mouse clicks using the famous puppeteer.

A quick recall!! Remember that puppeteer is headless chrome and can perform what a normal browser can perform when instructed by a user and one of them is mouse click.

Of course!, I hope someone isn’t waiting for me to explain how a mouse is used (mouse 101)…hahaha 😂☺

Well, if it’s your first time reading about the amazing Google puppeteer, I recommend you check out my recent articles on this blog.

We are going to look at a little example here which explains the entire concept so you will be able to implement in your projects.

The puppeteer API uses the mouse.click() to perform mouse click on any web page like a normal user.

mouse.click() method takes as parameter, the coordinates of the area to be clicked and the button click of the mouse.

In this case, we will;

-    Launch the headless chrome browser,

-    Open a new page or tab,

-    Set screen size,

-    navigate to the required url or website and then perform the click,

-    Takes a screenshot of the page and saves in the uploads folder

NB: The url or website used in this example is a website which tracks mouse click events and logs a statement after every mouse click.

In your IDE, create a file server.js and type the code below;

const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
// set the viewport so we know the dimensions of the screen
await page.setViewport({ width: 800, height: 600 })
// go to a page setup for mouse event tracking
await page.goto('http://unixpapa.com/js/testmouse.html');
// click an area
await page.mouse.click(132, 103, { button: 'left' });
// the screenshot should show feedback from the page that right part was clicked.
await page.screenshot({ path: './uploads/image.png' })
await browser.close()
console.log ('done');
})()

Run the script file,

How to perform mouse operations using node js and puppeteer

If everything works successfully, the word done, will be printed out on the console.

OUTPUT

How to perform mouse operations using node js and puppeteer


As you can see the image above, a mouse click was performed on the webpage.

Thanks for coding with me..
Chaow!!

Thanks for reading!

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