Making a Whatsapp Spammer Using Selenium and Python.
Tags: python, selenium, automation
Published: 2021-03-08 04:14:24

We all have had spamming wars on our group chats before, and they get tiring because of the sheer mundaneness of the task, resending a single message again and again. That's where automation steps in.

Selenium is a web testing library for the robot framework. We're going to be using it today.

1. __Install Selenium:__

 - Open a command line with admin privileges.
 - Type in 'pip install selenium'
```
pip install selenium
```

2. __Install Chrome Webdriver:__
 - Identify the version of chrome you are using
 - Go to [the chrome driver web page](https://sites.google.com/a/chromium.org/chromedriver/downloads)
and download the required driver.
 - Here as I am using a Windows OS, I downloaded the Win32 zip file.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8u1tkgyg1c1sghlnxf8.png)

 - After it has been downloaded, extract the .exe file and put it in a drive and file of your choice (It's important to remember where you have saved it).

3. __Check Environment:__
 - Go to a python editor of your choice (I use juypter notebook) and try importing selenium. If it runs without error, your setup is successful.

4. __The Code:__
 - Now copy down the following code:
```python
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe" # Location of the webdriver file

driver = webdriver.Chrome(PATH)
driver.implicitly_wait(15) 
driver.get('https://web.whatsapp.com')
driver.find_element_by_css_selector("span[title='" + input("Enter name to spam: ") + "']").click()
inputString = input("Enter message to send: ")
while(True):
    driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]').send_keys(inputString)
    driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button').click()
```
__IMPORTANT NOTE__ : Please change the PATH to where you have saved the .exe file.

5. __The Final Outcome:__
 - Using your phone scan the QR code for WhatsApp web and then enter the name and the message you want to spam.


__Spammer in Action:__

- Copy and paste the code from above into your editor ( Preferably Juypter Notebook).

- Run the code. You should be able to see something like the image below. You will also see that a new WhatsApp window opens up.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrrvnaitq0pdm51cimeb.png)
 
- In the 'Enter the name to spam' Section', add the name of the contact or group you wish to spam.

- In the 'Enter the message to spam', enter the string you wish to send.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ng7z7e2qfz8gykvqdjvn.png)

- After you hit enter, check the Whatsapp window, you will see an infinite number of messages being sent to the group/contact.

- To stop hit Ctrl + C or click on the stop button if you're on Juypter Notebook.


__Conclusion:__

The spammer works just as intended, and is extremely fast. It also provides a good eye-opener into the world of automation.

__Commonly Faced Errors:__

- Sometimes, when importing selenium, you may get an import error. Check where selenium is installed and figure out where if the selenium installation and python path match. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pswh287qnpiluohpod0f.png) 
 
Happy Coding!

 - Follow me on [Linkedin](https://www.linkedin.com/in/pranav-viswanathan-7976711b7/)
 - Follow me on [Github](https://github.com/PranavViswanathan)