Automate Whatsapp Messages

Vikas Jha
4 min readSep 28, 2020

--

In this article I am going to share steps on how we can Automate sending Whatsapp Messages. Before we go any further about automating the stuff, Let’s try to understand the steps involved in sending whatsapp messages manually from your computer.

Part 1: Opening web.whatsapp.com and scanning QR code to login. Notice this step needs to be performed only once and then these information gets stored in cookies. So If we open web.whatsapp.com from second time onwards then we don’t need to scan QR code any longer.

Part 2: This part has three steps.

Step 1 is clicking on the contact list to whom you want to send message.

Step 2 is typing your message in the text box area.

Step 3 is clicking on the send Icon to send message.

Since we now know the steps involved for sending message manually we can summarize that we need to know how to automate below four steps:

Let’s start coding and build our program which will perform all the above steps. I have pasted complete program below.

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

jokes = ["You don't need a parachute to go skydiving. You need a parachute to go skydiving twice.",
"Women call me ugly until they find out how much money I make. They they call me ugly and poor."]

options = Options()
options.add_argument("--user-data-dir=chrome-data")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome('D:\\pythoninstaller\\chromedriver_win32\\chromedriver.exe', options=options)
driver.maximize_window()
driver.get('https://web.whatsapp.com') # Already authenticated

time.sleep(20)

##################### Provide Recepient Name Here ###############################
driver.find_element_by_xpath("//*[@title='MyJakartaNumber']").click()

for joke in jokes:
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]').send_keys(joke)
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button/span').click()
time.sleep(10)

time.sleep(30)
driver.close()

Now, Let’s break it down in 7 steps and and understand the program in detail.

Step 1: Importing time and selenium Library. Time library will be used to perform sleep operations between operations. Selenium Library will be used to perform Webscraping and web automation.

Step 2: We keep list of all jokes in a variable called “jokes”. Now this can be any list of messages which you want to send. This can also contain something dynamic such as latest product price, stock price, currency exchange rate etc. It just depnds on what is the use case you have.

Step 3: In this step we are telling chrome about cookies location and some other options which are important. I don’t want to complicate by discussing this in more detail.

Step 4: Here we are pointing to chrome driver. Please note that you will need to download chromedriver.exe which is suitable with the version of chrome you have installed on your computer. We are also telling the URL of web.whatsapp.com here. “driver.get” operation is going to launch a chrome browser session and going to open web.whatsapp.com for us. At this point of time you will need to scan the QR code if you are running this program for the first time.

Step 5: We are performing the steps of identifying contacts element and clicking on the the recipient name. Please note in this case recipient name is “MyJakartaNumber”. You can replace this with any contact name or group name you want to send message to.

Step 6: In this step, we are performing a loop operations to loop through all the jokes we have. Each joke is going to be written in the text box and send operation is going to be performed. Please note that I have put a sleep of 10 seconds in the loop. It means that from one joke to another there is going to be a delay of ten seconds.

Step 7: In this step we are just giving 30 seconds of sleep before closing chrome session.

Hopefully you liked the program and understood it. If you have any queries or suggestions, feel free to write to me.

You can also watch the video on my Youtube Channel to understand the whole process in more detail and see the program executing.

--

--

Vikas Jha

I work on IT Technologies such as Automations tools, Reporting and Visualizations, Containerization, Microservices and of-course cloud.