Simple Whatsapp Automation Using Python3 and Selenium

Dea Venditama
Analytics Vidhya
Published in
3 min readApr 11, 2019

--

In my stories before (https://medium.com/@DeaVenditama/how-i-build-whatsapp-automation-tools-74f10c93e9b8) I tell you the general steps how to do it, now I will technically explain how to do it in code. I use Python 3 and Selenium in this tutorial, and I assume the reader knows the basics of Python.

Photo by Dominik Scythe on Unsplash

First of all, we must have Python 3 installed; you can download it from https://www.python.org/ and follow the install instruction. After Python3 installed, install Selenium Automation Framework to automate all the things that we want to do later. I recommend using pip to install Selenium.

python3 -m pip install Selenium

Selenium Hello World

After Selenium successfully installed, to test if it is correctly installed on your machine or not, run this Python code and check if there are any error messages.

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("http://google.com")
time.sleep(2)
driver.quit()

Save this code in a file called automate.py or any python file name and then run it, it will show Google Chrome Window and automatically go to google.com.

Automate Whatsapp

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
contact = "John"
text = "Hey, this message was sent using Selenium"
driver = webdriver.Chrome()
driver.get("https://web.whatsapp.com")
print("Scan QR Code, And then Enter")
input()
print("Logged In")
inp_xpath_search = "//input[@title='Search or start new chat']"
input_box_search = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search))
input_box_search.click()
time.sleep(2)
input_box_search.send_keys(contact)
time.sleep(2)
selected_contact = driver.find_element_by_xpath("//span[@title='"+contact+"']")
selected_contact.click()
inp_xpath = '//div[@class="_2S1VP copyable-text selectable-text"][@contenteditable="true"][@data-tab="1"]'
input_box = driver.find_element_by_xpath(inp_xpath)
time.sleep(2)
input_box.send_keys(text + Keys.ENTER)
time.sleep(2)
driver.quit()

copy the code above and paste it in automation.py, run it, and it will show a Whatsapp Web interface. Scan the QR code, wait until the loading is complete and press enter on the terminal/command prompt to continue the process to send the messages.

This is the explanation of Python code above,

contact = "John"
text = "Hey, this message was sent using Selenium"

these two lines of code is a Python variable to store the name of the contact and the messages.

driver = webdriver.Chrome()
driver.get("https://web.whatsapp.com")
print("Scan QR Code, And then Enter")
input()
print("Logged In")

It will open a Whatsapp Web interface which automatically asks you to scan the QR code, input() is used to pause the process until you press enter button in terminal.

inp_xpath_search = "//input[@title='Search or start new chat']"
input_box_search = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search))
input_box_search.click()
time.sleep(2)
input_box_search.send_keys(contact)
time.sleep(2)

the inp_xpath_search variable is an XPath path which used to find search input box. After the search box found it will click the search box, input the name of the contact and then enter it automatically.

selected_contact = driver.find_element_by_xpath("//span[@title='"+contact+"']")
selected_contact.click()

Click the contact which found in the search result

inp_xpath = '//div[@class="_2S1VP copyable-text selectable-text"][@contenteditable="true"][@data-tab="1"]'
input_box = driver.find_element_by_xpath(inp_xpath)
time.sleep(2)
input_box.send_keys(text + Keys.ENTER)
time.sleep(2)

Find the message text box, enter a message and press enter automatically

driver.quit()

driver.quit() used to kill the instance of chromedriver or any driver you used, and if you don’t include this line, the process will always running in your computer memory.

Ok, this is the end of the tutorial, in next stories I will show you how to wrap the code in a Python class so you can use it more dynamic and reusable. Maybe I will cover some new feature like scraping contact or messages.
Thank you….

--

--

Dea Venditama
Analytics Vidhya

Freelance Programmer & Fungsional Pranata Komputer Badan Pusat Statistik RI