Automate Whatsapp Messages With Python

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.

Image for post

Part 2: This part has three steps.

  • First Step is to click on the contact list to whom you want to send message.
  • Second step is to type your message in the text box area.
  • Third and last step is to click on the send Icon to send message.
Image for post

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:

Image for post

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

Complete Program to Automate Whatsapp Messages Using Python

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.

Python Code To Automate Whatsapp Messages
Python Code To Automate Whatsapp Messages

Explanation of Automating Whatsapp Messages Using Python Program

Importing required modules

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.

Prepare List of Messages or Jokes to be sent

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.

Let chrome know about cookies location so we don’t have to Scan QR Code again

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.

Launch chrome browser and open web.whatsapp.com automatically

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.

Identify receiver

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.

Loop through all the jokes

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.

Just give some wait time and close chrome

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. You may read about more automation ideas using python by clicking here. 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.

If you liked this than you may also like below Automation Posts:

  • Python Pandas Frequently Asked Questions
    Learn Python Pandas Frequently Asked Questions and increase chances of selection during interview. These are all the concepts you need to learn about Pandas Dataframe to become an expert. So let’s get started. Note: In this article we are using data from this sample file to explain the concepts. You may also refer to this …

    Python Pandas Frequently Asked Questions Read More »

  • Python Automation Ideas – Beginners Level
    PYTHON AUTOMATION IDEAS FOR BEGINNERS WITH EXAMPLES As a python enthusiast or as a python student you would be looking for ideas which you can automate. So in this article we are going to discuss about Python automation ideas for beginners with example. These ideas or projects are like building blocks for other advance automation …

    Python Automation Ideas – Beginners Level Read More »

  • Automate Whatsapp Messages With Python
    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 …

    Automate Whatsapp Messages With Python Read More »

  • Python Datatypes
    Welcome to Python tutorials for beginners. In this tutorial you are going to learn a lot about Python datatypes. Here is the list of all the supported Python datatype which are typically used in order to create an application or a website. Python for beginners complete course Python String Datatype Python string datatype is most …

    Python Datatypes Read More »

  • Python Frequently Asked Questions
    In this article, I have compiled all the python frequently asked questions. This will help you to start your career in Python. Get complete Python course at special price from here. If you are not from computers background then you must be thinking what is this Python Buzz all about? So in this article we …

    Python Frequently Asked Questions Read More »

  • Python For Beginners
    In this Python Programming For Beginners article you will learn about Programming step by step. It is always best to start with frequently asked questions. Once you know frequently asked questions then you know that you base is set right. I recommend to follow along with this tutorial by doing actual hands on. Watching Python …

    Python For Beginners Read More »

  • Python Example Codes
    Python Program to Automate Sending Messages on Telegram Groups: Here is the High Level Steps Description: Step 1: Telegram Group In Which You will send MessagesStep 2: Telegram Bot & add bot in the Telegram groupStep 3: Chat ID (-393992542)Step 4: https://api.telegram.org/bot1285167174:AAGrzty80DdyRnjnh7NrvXKjd4I4_26UKf4/getUpdatesStep 5: https://api.telegram.org/bot1285167174:AAGrzty80DdyRnjnh7NrvXKjd4I4_26UKf4/sendMessage?chat_id=-393992542&text=”This is a test Message” Here is Python Code: import requests import …

    Python Example Codes Read More »

  • Youtube Views
    Here is fully working python program for Youtube views with step by step explanation. Here is the complete Python Program Code: Python Program to Send Automatic Messages Using Telegram Bot Complete Code is available Here Watch Complete Python Playlist and Be an Expert in Python

Leave a Comment

%d bloggers like this: