Improve Your Personal Relationships — With a Touch of Automation and Python
By: Rajat Mehta
Tags: Python, Programming, Selenium, Relationship Building, Automation

Get your coffee ready, it’s time to convert caffeine into code.

Step 1. Preparing the data

The data is kept straightforward. Only a CSV with 2 columns - the recipient’s details (mobile number or group name) and the message - is needed. Here is the sample dataset -

Step 2. Installing Selenium

Use pip install selenium to install the library. Selenium requires a driver to interact with the browser of your choice. In this example, we’ll use Firefox. You can find URLs to download drivers here — https://pypi.org/project/selenium/

Geckodriver is the driver for Firefox which can be downloaded here-https://github.com/mozilla/geckodriver/releases. Download the latest version and add Geckodriver to PATH variable of the OS.

P.S. — Ensure that you have the latest Firefox and geckodriver is in PATH variable to prevent any errors at a later stage.

Step 3. Understand a few Selenium basics

Selenium automates browsers. Any action like click, scroll or type etc can be performed using Selenium which makes it perfect for our needs. Before we dive into the coding lets understand the basic working of Selenium.

Since we automate the actions on the browser, first we need to identify which element of the webpage we need to act on. There are multiple ways of finding the element in Selenium. A thorough guide can be found here.

For automating Whatsapp we’ll use XPath. In simple words, XPath is used to identify the element on the basis of its tag name and its associated attributes. Its syntax is something like this:

xpath = //tag_name[@attribute_name = “attribute_value”]

Let’s take a very simple HTML example:

<div class = “highlighted text” id = “block 1”>

If we need to find this element on the page and we know that id attribute uniquely identifies it then a simple XPath expression to find would be:

xpath = //div[@id=“block 1”]

Isn’t that pretty neat?

Let’s test this knowledge on web.whatsapp.com. Open the website and scan the QR code. Once you see your chats press Ctrl+Shift+I to inspect the webpage. It should look something like this -

Image taken by author.

We need the XPath for 2 elements - the search bar to type the recipient’s name and the message bar to type the message. On selecting both these bars in inspection mode, there’s one common attribute -

contenteditable = “true”

Using this attribute we can get the XPath for them and perform our actions.

Step 4. THE CODE

The code is rather straightforward. After importing the libraries and loading the CSV, we open the browser and let it go to web.whatsapp.com. Point to note here is that we need to scan the QR code every time we go to web.whatsapp.com. So, Selenium is paused for 10 seconds until we scan the QR code using the time module’s time.sleep(10) . Line 22 applies a function send_message to all rows from the CSV.

send_message imitates how we would send messages. On receiving the recipient and the message, it first finds the search bar to input the message. Since contenteditable = “true” can retrieve the search bar, we use browser.find_elements_by_xpath() function. The first element of the resulting list will be the search bar and the second element will be the message bar.