Automate WhatsApp with PyAutoGUI

Mithu Mithu Nov 13, 2019 · 3 mins read
Automate WhatsApp with PyAutoGUI

Automating WhatsApp with PyAutoGUI

Hey guys, What’s up? LOL.
Now that you’re here, Let’s do magic.  😜

In this tutorial we’ll be using PyAutoGUI to create a simple program to send message(s) on whatsapp automatically at a given time.

For example:
Say, you want to send someone a birthday wish whenever the time is exactly 12:00am, You could wake up all night or, use this program to automate the process.

PyAutoGUI is a cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.

Full documentation available at:

Before you proceed, Keep in mind that while working with pyAutoGUI, you might encounter wierd problems such as unintentional mouse movements or keyboard inputs.
You must be able to overcome these.
Don't worry It won't in any way destroy your computer.
A simple sign out and re-sign in (or shutdown) will fix it.

Let's get started.

I assume, You've the latest version of python installed on your computer. If not,

The first step is to install the module.
To install PyAutoGUI, Launch Command Prompt and execute the following command(s):

If you're on Windows:

pip install pyautogui

On Linux, this is:

pip3 install python3-xlib
sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev
pip3 install pyautogui

Check if pyautogui is installed succesfully,
 - Launch Python IDLE and execute the following commands:

import pyautogui

That should return the current position of your mouse on the screen. (As shown below)

PyAutoGUI installation check

If you're having trouble with the installation check out:

Now, make a folder, and create a python file called

In, import the pyautogui, time, and the webbrowser modules

#import the pyautogui, time, and the webbrowser modules
import webbrowser
import pyautogui as pygui, time 

The webbrowser module will be used to open Whatsapp Web in your default browser.
And, the time module, for delaying pyautogui until the page is fully loaded.

#open Whatsapp Web on default web browser'')
#wait for 10 seconds. For slower connections, increase the value of sleep

Next, set the contact and message to be sent.

contact = "787*******"  #can be name or a number
message ="Good Night!!!"

Now, here comes the tricky part,
You'll have to determine the position of the search bar in WhatsApp Web (marked in yellow).

WhatsApp search bar position

In my case the position was x=142, y=188
That depends on the screen size of your computer.
It'll probably be around those values.

This moves the position of your mouse to the search bar and clicks on it.

pygui.moveTo(x=142, y=188, duration = 0.7)

This will enter the value of contact to the search bar and hit enter which ends up opening the conversation.

pygui.typewrite(contact, interval = 0.04)'enter')

And, finally, let's send the message.

pygui.typewrite(message, interval = 0.03)'enter')

The final code should look something like this:

#import the pyautogui, time, and the webbrowser modules
import webbrowser
import pyautogui as pygui, time 

#open Whatsapp web in default web browser'')

#wait for WhatsApp web to load completely
#Increase for slower connections. current is 10s

contact = "787*******"    #this could be name or number
message ="Good Night!!!"

#move to search bar and click
pygui.moveTo(x=142, y=188, duration = 0.7)

#enter contact name or number and hit enter.
#this opens the conversation
pygui.typewrite(contact, interval = 0.04)'enter')

#type the message and send
pygui.typewrite(message, interval = 0.03)'enter')

Run the program by executing the command,


(open CMD/Terminal in the directory your file is in.)

And, That was just an example of how powerful pyAutoGUI module is. Go ahead and experiment with it. You can come up with some cool ideas. 

You could create a program that could draw, or something that fills up online forms automatically, take screenshots and what not? The possibilities are endless....


Thanks for reading. That's all for now. Hopefully, you learnt something out of this. 😊
-Mithu Mondal


Join Newsletter
Get the latest news right in your inbox. We never spam!
Written by Mithu Mondal Profile
Something went wrong. Please check your internet connection.

Connect with Mithu