Automating Whatsapp and Instagram with Selenium (Python Package)

Koushal Bhat
4 min readJan 10, 2021

Recently I learned about a cool python package Selenium which is used to automate web browser interaction from Python. Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer), as well as the Remote protocol.

Selenium is a very smart package in python with which Users can automate the browser’s activity.

And with the help of this package, I tried to automate Whatsapp-Web. I found out that the internet already had a lot of stuff regarding this so I looked upon that but failed to automate WhatsApp.

I faced some errors like

raise TimeoutException(message, screen, stack trace)

TimeoutException: Message:

StaleElementReferenceException: The element reference of e75a1764-ff73–40fa-93c1–08cb90394b65 is stale either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed

After searching about these errors over StackOverflow and everywhere. I was finally able to build a correct python script with the help of which I automated my WhatsApp-web and in the same course I tried to do the same thing with Instagram DM’s too.

Requirements :

For this Automation Two main requirements are there :

  • Selenium (Python Package)

Installing Selenium-Selenium can be installed using the following code on the terminal with the help of “preferred installer program” pip in python-

pip install selenium

  • Chromedriver(for web automation)

Downloading Webdriver- Web Driver is required for automation. We can Download the chrome web driver from this link ( download the appropriate version as per your PC).

Procedure:

Now that we are done with basic setup, Now it’s time for some coding

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

driver=webdriver.Chrome(“/home/koushal/selenium/chromedriver”) #chromedriver path

driver.get(“https://web.whatsapp.com/")

name=input(“Enter the Target Name after scanning QR code.\n “)

user = driver.find_element_by_xpath(“//span[@title=’{}’]”.format(name))

user.click()

text_box=driver.find_element_by_xpath(‘//[@id=”main”]/footer/div[1]/div[2]/div/div[2]’)

for i in range(500):

text_box.send_keys(“Good Morning”+Keys.ENTER)

driver.quit()

The above code helps us to send “Good Morning ” text message to the target user for 500 times with one click.😅😅

You can Find My full Code at https://github.com/koushal2001/selenium

First of all, I have imported the webdriver package from selenium which helps us to manage our web browser.

Then I have imported Keys which are referring to keyboard keys. It’s used to ID the different Keys on the keyboard.

Ex: Keys.ENTER means your ENTER key in your keyboard. These keys can then be used by selenium for keyboard emulation or other stuff.

We use the browser as the object which uses the functionality of the chrome web driver and ‘/home/koushal/selenium/chromedriver’ is the location of the web driver.

We input the name of the target after scanning the QR code.

user = driver.find_element_by_xpath(“//span[@title=’{}’]”.format(name))

user.click()

This part of the code finds the contact with that name and clicks and opens that chat.

XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document”

Except for XPath there are other elements too through with we can search on a web page like

driver.find_element_by_css_selector()

driver.find_element_by_name()

driver.find_element_by_xpath()

driver.find_element_by_class_name()

driver.find_element_by_id()

etc…

text_box = driver.find_element_by_xpath(‘//*[@id=”main”]/footer/div[1]/div[2]/div/div[2]’)

for I in range(500):

text_box.send_keys(“Good Morning”+Keys.ENTER)

This part of the code find the text box and sends our text there after which the Keys.ENTER part simulates the click of ENTER and our message is sent.

As in the for loop it’s repeated 500 times, can be sent one time only.

Done with WhatsApp Part …………………………………………

Did a similar thing with Instagram with the following python script

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

driver=webdriver.Chrome(“/home/koushal/selenium/chromedriver”)

# Selenium chromedriver path

driver.get(“https://www.instagram.com/direct/inbox/")

input()

user = driver.find_element_by_css_selector(“#react-root > section > div > div.Igw0E.IwRSH.eGOV_._4EzTm > div > div > div.oNO81 > div.Igw0E.IwRSH.eGOV_._4EzTm.i0EQd > div > div > div > div > div:nth-child(1)”)

user.click()

text_box = driver.find_element_by_xpath(‘//*[@id=”react-root”]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea’)

for i in range(0,50):

text_box.send_keys(“Dummy Script Test”+Keys.ENTER)

This sends message to the most recent contact in your chat list.

Here in placeof XPath i have used CSS_Selector And rest of the part is similar.

To get the XPath or CSS_Selector of a particular element on a webpage we can Right-click on that element and then click on the inspect element section from where we can easily copy these.

Ps. Please use this script only for educational purposes, I am not responsible if your friends ( or even Whatsapp ) may block you.

Comment below about your experience!

And do clap if you like the content. Peace!

--

--