Selenium Python WhatsApp Automation Made Easy

Do you want to automate your WhatsApp messages to annoy your friends or just for fun or to learn selenium python? If yes then this article is for you in this article, I’ll show you how you can automate your WhatsApp messages using selenium python.

annoy WhatsApp friends using selenium python




Install Selenium Python

In order to use selenium first, we need to install it on our machine. Since selenium is a python library make sure python is installed in your system and pip is configured properly. Just type the command given below to install selenium in python that is pip install selenium .

pip install selenium #installs selenium




Download Chromdriver

The next thing we need for browser automation is chrome driver to automate the Chrome Browser using selenium. Click here to visit the chrome driver download page and download the zip file of the chrome driver according to your chrome browser version. After it is downloaded extract it to the convenient path cause we will need the path for the chrome driver in our code.

Automating WhatsApp Messages Using Selenium Python

Open WhatsApp using Selenium Python
from selenium import webdriver
driver=webdriver.Chrome('chromedriver.exe')
driver.get("https://web.whatsapp.com/")

Type this code in your code editor and run it. This code will open the chrome browser and then visit the WhatsApp webpage automatically with the help of selenium python.

Here in the first line we import webdriver from the selenium package and then in the second line we assign the path of the chrome driver that we downloaded earlier to the driver. Then in the third line, we tell the driver to get us a WhatsApp webpage using get that accepts the web address of any site. Since I’ve extracted chromedriver.exe in the same folder of this code so I just used ‘chromdriver.exe' as for the path, if you have extracted it somewhere else make sure to add the whole path in your selenium python code.




After visiting the webpage you may see that you need to login to WhatsApp again or all your chrome data like cookies are not in this browser that we opened using selenium python. To load all the data from our chrome browser using the following code.

from selenium import webdriver

options=webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/Aman/AppData/Local/Google/Chrome/User Data")
driver=webdriver.Chrome('chromedriver.exe',chrome_options=options)
driver.get("https://web.whatsapp.com/")

Here we loaded all of our chrome data using webdriver.ChromOptions() class that we assigned to options and then with the help of add_argument function we assigned path for the user data of the chrome browser (Make sure to change the path of the user data to your own path). After that, in line 5 while opening the browser using the driver we added one more argument that is chrome_options=options to make sure to load all the chrome data while opening the chrome browser.

Now you may see that your WhatsApp account has been logged in automatically if it was already logged in, If it wasn’t then it’s time to log in to your WhatsApp now!

Send messages automatically Using Selenium Python

Now that we’ve logged in to our WhatsApp account, let’s bomb messages to someone using the following code.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

options=webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/Aman/AppData/Local/Google/Chrome/User Data")
driver=webdriver.Chrome('chromedriver.exe',chrome_options=options)
driver.get("https://web.whatsapp.com/")
wait=WebDriverWait(driver,100)

target='"Your Target"'

contact_path='//span[contains(@title,'+ target +')]'
contact=wait.until(EC.presence_of_element_located((By.XPATH,contact_path)))
contact.click()
message_box_path='//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]'
message_box=wait.until(EC.presence_of_element_located((By.XPATH,message_box_path)))

Now in the above code, you may see that we’ve imported a few more different things from selenium like WebDriverWait, expected_conditons, and BY.

  • WebDriveWait class is used to wait for the specified seconds. It accepts two arguments driver and time. Time is entered in seconds.
  • expected_conditions used to perform explicit wait on certain conditions.
  • BY class is used to locate elements.

In line 10 we defined wait using WebDriverWait to wait for 100 seconds before throwing out an error. Since all elements of the webpage are not loaded instantly we can’t find the elements that we need instantly. So we defined wait to wait for 100 seconds to find the elements that we searched in lines 15 and 18.

In line 12 we provided our target and then in line 14, we defined the XPATH of the target and then searched for our target in line 15 using the XPATH that we defined earlier for our target. In line 16 after finding our target, we clicked on it to open its chat.

Now we defined the XPATH of the message box in line 17 and then searched for the message box element in line 18. We used wait.until to wait until the element is located and the expected condition is to locate the element by XPATH that we defined in the previous line.

Now that we’ve located our target and message box it’s time to send messages. So for that, we added the following lines to our existing code.




Final Script Of Whatsapp Automation Using Selenium Python
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

options=webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/Aman/AppData/Local/Google/Chrome/User Data")
driver=webdriver.Chrome('chromedriver.exe',chrome_options=options)
driver.get("https://web.whatsapp.com/")
wait=WebDriverWait(driver,100)

target='"Your Target"'
message="Your Message"
number_of_times=10 #No. of times to send a message

contact_path='//span[contains(@title,'+ target +')]'
contact=wait.until(EC.presence_of_element_located((By.XPATH,contact_path)))
contact.click()
message_box_path='//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]'
message_box=wait.until(EC.presence_of_element_located((By.XPATH,message_box_path)))
for x in range(number_of_times):
    message_box.send_keys(message + Keys.ENTER)
    time.sleep(0.2)

Here first we imported KEYS and time in our selenium python code.

  • KEYS are used to represent keyboard keys.
  • time provides various time-related functions.

Then in line 15, we defined a message variable that’ll contain that message, and then we defined the number of times to send the message in line 16.

Find Social Media Accounts With Sherlock

In line 23 we used for loop to send the desired message as many times as we want and in that loop, we used .sendkeys that accept the message and we can also provide a keyboard key using the KEYS that we imported. So we added our message variable and then Enter key after our message to send it since we can use enter key in WhatsApp to send a message. After that we used time.sleep to wait for 0.2 seconds before sending the message again.




That’s it run this script and then you’ll be able to automate WhatsApp using selenium python. You can make a lot of changes to this script to customize it according to your need.

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x