How to annoy your friends on WhatsApp?
Tags: whatsapp, selenium, chrome, automation
Published: 2021-06-25 08:05:15

There are various ways to annoy your friends but being a programmer(); I should do it with my weapon known as CODE. 👨‍💻

Let's dive into it. 😁

We will be using `python` as a programming language and `selenium` to automate messaging. 

### Step 1: Download Chromedriver

Use [this][chromedriver] link to download ChromeDriver and extract the zip file. <br>
Note: Download according to the version of Chrome installed on your system.

### Step 2: Install Selenium Package

We need to install the **selenium** python package.<br>
Open the terminal and run the command `pip install selenium`.

Learn more about Selenium [here][selenium]

### Step 3: Programming

*Time to code ();* 😉

- Create a python file `filename.py` in any code editor. (I am using [PyCharm][pycharm])
- Paste the following code (Read the comments to understand the code)
- [How to get XPath of HTML element?][xpathblog]

```Python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

# About Selenium - https://www.selenium.dev/

# Getting Chrome Driver
driver = webdriver.Chrome(r"D:\Softwares\chromedriver_win32\chromedriver.exe") #put path of chromedriver.exe
driver.get('https://web.whatsapp.com/')

# Number of messages you want to spam
MESSAGE_COUNT = 10

# Sends Message
def sendMessage(msg):
    # Entering message in chat box
    WebDriverWait(driver, 100).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'))).send_keys(msg)

    # Clicking SEND button
    WebDriverWait(driver, 100).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'))).send_keys(
        Keys.RETURN)

flag = True

while flag:
    name = input("Enter Contact Name (Type \'exit\' to end the program) :") #type exit to end program

    if name == "exit":
        flag = False
    else:
        # How to get XPath of HTML element? -
        # Learn at https://ajaygalagali.hashnode.dev/how-to-get-xpath-of-html-element

        # To know more about XPATH visit - https://developer.mozilla.org/en-US/docs/Web/XPath
        
        # Clicking on Search
        WebDriverWait(driver, 100).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="side"]/div[1]/div/label/div/div[2]'))).click()

        # Entering contact name
        WebDriverWait(driver, 100).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="side"]/div[1]/div/label/div/div[2]'))).send_keys(name)

        # Opening chat of contact
        WebDriverWait(driver, 100).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="side"]/div[1]/div/label/div/div[2]'))).send_keys(
            Keys.RETURN)

        # Spamming messages
        for i in range(MESSAGE_COUNT):
            sendMessage(i)

        sendMessage("These messages are sent by Python Program!")
        sendMessage("Learn here: https://ajaygalagali.hashnode.dev/")

``` 
See to it that package is imported successfully. 

### Step 4: Sending messages

- Run the program
- WhatsApp Web will be opened in the new Chrome Browser window.
- Scan the QR code by your smartphone WhatsApp app to log into your account.

![WhatsApp Web QR Code](https://cdn.hashnode.com/res/hashnode/image/upload/v1624441185510/Bln9t32MF.png)

-  Enter the contact name you want to send messages to

![Enter the contact name you want to send messages to](https://cdn.hashnode.com/res/hashnode/image/upload/v1624441325458/lUSWALwvE.png)

- Selenium will search the contact, types the messages, and sends them automatically.

![Output of the program](https://cdn.hashnode.com/res/hashnode/image/upload/v1624441393918/Jg1XHBoGZ.png)
 
- Here, I sent 11 messages for demo purposes. You can send 1000s of messages too. 

- You can change the variable `MESSAGE_COUNT`  in the program to any number, that number of messages will be spammed to your friend. 

### 👨‍⚖️ Conclusion

Are they gonna get annoyed by just 11 messages? No way! <br>
Listen to my story! I programmed to send 10k messages to my close friend's group. Around 50 messages were sent into the group and admin **kicked** me out of the group 😂. Admin with a brain! 👏. 
Well, title should be *How to get kicked out of WhatsApp group without being mean?* 😁


![removed from group](https://cdn.hashnode.com/res/hashnode/image/upload/v1624442657092/PwMPkyGbSd.jpeg)

lol xD

---
# 🤝 Thank You
> Keep annoying friends 😉

⚠ Check out original blog [here](https://ajaygalagali.hashnode.dev/how-to-annoy-your-friends-on-whatsapp)

[chromedriver]:https://chromedriver.chromium.org/downloads
[chrome]:https://www.google.com/intl/en_in/chrome/
[pycharm]:https://www.jetbrains.com/pycharm/download/
[selenium]:https://www.selenium.dev/
[xpathblog]:https://ajaygalagali.hashnode.dev/how-to-get-xpath-of-html-element