# WhatsApp Bot for Auto Replying & Sending Images via Python and Selenium

Are you someone who wants to send lots of quotes or *shayaris* (poetry) to your ex or to a WhatsApp friends or colleague group? Or maybe random emojis to your wife or latest girlfriend? This blog will help you out in programming a bot to impress them.

Here comes the demo:

Bot performing auto replying and login

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052509158/884bc493-775a-4b55-9224-91ae81eded8f.png align="left")

For implementing this, you will need Jupyter Python notebook installed. You can use this procedure illustrated over here for installation on Windows or Linux:

[**How To Set Up Jupyter Notebook with Python 3 on Ubuntu 18.04 | DigitalOcean**  
\*This tutorial will walk you through setting up Jupyter Notebook to run from an Ubuntu 18.04 server, as well as teach…\*www.digitalocean.com](https://www.digitalocean.com/community/tutorials/how-to-set-up-jupyter-notebook-with-python-3-on-ubuntu-18-04)

Also, we will be using Selenium, which is an automatic testing tool compatible with Java, Ruby, Python, and many other programming languages.

In order to install the selenium dependencies we will use pip (package installer for Python):

`!pip install selenium`

`!pip install webdriver_manager`

For every block to run, we will use Shift+Enter to execute the relevant command.

Along with this, you will import the files like this :

`from selenium import webdriver   from webdriver_manager.chrome import ChromeDriverManager   import random   from selenium.webdriver.common.keys import Keys`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052511137/cf10dec3-1b09-42ce-a50b-3b89179e2893.png align="left")

Python Dependencies installation

In order to open Google Chrome from Jupyter notebook automatically, we will use “chrome” element as an object from webdriver package and give it a relevant URL to open (Web.whatsapp.com for WhatsApp web).

`chrome = webdriver.Chrome(ChromeDriverManager().install())`

`chrome.get(“`[`https://web.whatsapp.com`](https://web.whatsapp.com)`")`

Once you execute this on Jupyter Notebook, you will open this :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052512861/501ed290-87c9-487e-be0a-ce44a94df623.png align="left")

WhatsAPP web opened from Jupyter

Once you have the QR code in front of you, scan your WhatsApp web and access your WhatsApp account like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052514802/7d3b6efa-962d-47d4-bfdc-7751614b0473.png align="left")

WhatsApp web after scanned

You will need to access the search contact bar to find contacts for starting a new chat.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052516498/a10f3dfc-cc90-4e91-8818-bb8f54edf769.png align="left")

You can right-click upon it, and select “inspect element”.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052518308/345349f1-084e-480b-ad21-205e4a5d74d8.png align="left")

Inspect element

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052520124/23418493-2dc7-4fc5-8b51-681cd9319f84.png align="left")

Element class name for Search bar

We can see that the class name for the search bar is:”\_3FRCZ”

Now we will extract the element using the Python Selenium packages and search for the group named Banglore :

`search_box = chrome.find_element_by_class_name(“_3FRCZ”)   search_box.send_keys(“Banglore”)   search_box.send_keys(Keys.ENTER)`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052522228/1039ac1d-bbde-4c49-baa9-f40dc0df0d34.png align="left")

Banglore searched and appears on the top

Once “Banglore” appears on the top, we will find its ID again with xpath since Calss and ID’s might create ambiguity:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052524409/2f84099a-e749-4dc7-80bc-f4800a8d12a3.png align="left")

We will fetch and write a Hello message 10 times using loop in the group:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052526433/e20e70de-b85c-47ff-8096-d5134c294227.png align="left")

Code for sending automatic Hello’s ,emotions and images

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052528090/7c96efed-d71b-476d-8ebb-3165d9c06d55.png align="left")

Similarly , we can give 10 randomly generated emotions in the group

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052529410/974bd64d-226d-4db0-aa4b-b5b5912d9030.png align="left")

Emotions

Now we can also send attachments as pictures,videos,camera pics or contacts ,by accessing the attachment symbol:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052531380/f07537be-550f-4259-a18c-03a6a9b164ab.png align="left")

Attachment symbol

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052533518/0c2a85d5-05e9-4c14-bb77-6ff027f4734e.png align="left")

Selecting the relevant things to attach

Once you get the xPath of icon photo attachment:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052535386/8d361cb9-4797-4376-a592-20366554bb2c.png align="left")

Script to attach the pics:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052537625/11deb838-4854-412c-9a39-799d6211a54a.png align="left")

Sending pics via bot

These are simple packages to autotest the modules created, if you want to implement it kindly refer to the code repository:

[**aniforverizon/WhatsAppbot**  
\*Permalink Dismiss GitHub is home to over 50 million developers working together to host and review code, manage…\*github.com](https://github.com/aniforverizon/WhatsAppbot/blob/master/acanubhav_WhatsApp_Chatbot.ipynb)

For any further queries or anything related to Blockchain or Devops, you can DM me on LinkedIn or on my Instagram (id=acanubhav94).

[**Anubhav Chaturvedi - Solutions Architect | Blockchain ,Devops,Visualization and Data Expert …**  
\*Working as an Independent Contractor for roles like Blockchain Development(Hyperledger ) and Devops Engineer including…\*www.linkedin.com](https://www.linkedin.com/in/anubhav-chaturvedi-a7465a72/)

*More content at* [*plainenglish.io*](http://plainenglish.io/)
