Send Multiple WhatsApp messages using Seleinum

 

I made a fun project with selenium. I wanted to prank 5 friends of mine by surprising them with 1000 messages popping up of their WhatsApp. I definitely can’t paste 1000 times* 5, so I started wondering is there a simpler way by coding it on python. Let me break down this simple code and explain how it works. I used Jupiter notebook for this entire fun project.

 

Step 1: # first and foremost, pip install selenium you will also find it here

Import web driver from selenium and time libraries. We shall be using them for this project.

### from selenium import webdriver
###  import time

Step 2: Now, download chrome web drivers from here. This driver is used for operating the web page with selenium.  You can also use firefox by downloading firefox web drivers, but for this project, I am using Chrome 🙂

Note: make sure to have the webdriver.exe and python script in the same folder. 

#### driver = webdriver.Chrome()       #a new chrome tab will open and wait for selinium commands.
#### driver.get(“https://web.whatsapp.com”)  #  whatsapp web will open
#### print(“Scan QR Code in 60 seconds”)         # scan the QR code in 60 seconds to login.
#### time.sleep(60)

When you execute the above code a new webpage pops up waiting for your QR code to scan. Once you finish the scan your notebook will get access to your WhatsApp.

 

Step 3: Now create a list and text you want to send to them. 

#### Now, I created a list of friends A,b,c,d,e.and the text  “hi guys”

#### Contact = [“A”,”B”,”C”,”D”,”E”,”F”]

Step 4: Conventionally, We search our contact list on WhatsApp web, for that we hit on the search box, type the contact name stored in our phone and hit enter. Similarly, we should guide our script to follow the same.

###### search = driver.find_element_by_xpath(“//span[@title='{}’]”.format(contact))                        # this part, looks for search box on whatsapp web and pastes your contact name in it.
###### search.click()                               #This part, Selects the contact name.

 

Step 5: Now this step might vary from system to system, on your webpage hit F12 to see the HTML code, search for “copyable-text selectable-text”. You will find this piece of code ‘//div[@class=”_3FRCZ copyable-text selectable-text”][@contenteditable=”true“][@data-tab=”6“]’  with different keys on your screen. Copy the line or make sure to change the attributes accordingly.

Note: Paste the line in html_link= ___________ below. 

#       html_link = ‘//div[@class=”_3FRCZ copyable-text selectable-text”][@contenteditable=”true”][@data-tab=”6″]’                     # your textbox path is given
#       text_box = driver.find_element_by_xpath(html_link)                                                                                                                      # your textbox path is selected here
#       for i in range(1000):                                                                                                                                                                             # runs loop 1000 times (sends message 1000 times)  we can change the number accordingly.
                   text_box.send_keys(text + Keys.ENTER)                                                                                                                                # under every loop the text we want to send will be sent to our folks. ( here for thousand time)

 

step 6: since we have a contact list of 5 we have to make few changes in the above code to make it work for multiple names. We do it by simply adding a for loop to it.

#       for i in range(len(contact)):                                 
                 search = driver.find_element_by_xpath(“//span[@title='{}’]”.format(contact[i]))
                 search.click()
                 html_link = ‘//div[@class=”_3FRCZ copyable-text selectable-text”][@contenteditable=”true”][@data-tab=”6″]’
                 input_box = driver.find_element_by_xpath(html_link)
                 for i in range(1000): 
                          input_box.send_keys(text + Keys.ENTER)

The entire code is pasted down below.


Conclusion: The above script was built to prank my friends. But this could also be used in serious business situations where few repeated messages must be sent to multiple contact persons on WhatsApp. This would be a hand tool and saves a lot of time.

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *