DEV Community

Cover image for WhatsApp Automation using Python and Selenium
Saurabh Kumar
Saurabh Kumar

Posted on

WhatsApp Automation using Python and Selenium

In this tutorial, I have used Python3 and Selenium. I am assuming that you know the basics of Python.

If you don't have Python installed, you can download it from https://www.python.org/ and follow the install instruction.

After Python installed, we will need to install Selenium Automation Framework. We will be using pip to install Selenium.

python3 -m pip install Selenium
Enter fullscreen mode Exit fullscreen mode

Next, we will need to install ChromeDriver. You can download it from https://chromedriver.chromium.org/downloads. After downloading, unzip it and move it to some sensible location.

Automate WhatsApp

import os
from typing import KeysView
from selenium import webdriver
from selenium.webdriver.common import keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

os.environ['PATH'] += r"C:\Users\saura\Desktop" #replace it with path that contains chromedriver
driver = webdriver.Chrome()

driver.get("https://web.whatsapp.com/")

driver.implicitly_wait(5)
try:
    destination = driver.find_element(By.XPATH, "//span[.='Friend']")#replace Friend with your WhatsApp contact
    destination.click()
except:
    destination = driver.find_element(By.XPATH, "//span[.='Friend']")#replace Friend with your whatsApp contact.
    destination.click()


msg_area = driver.find_element(By.CSS_SELECTOR, "[title*='Type a message']")
for i in range(20):    #Number of time message will be sent.
    msg_area.send_keys("Hi")     #Replace "Hi" with whatever message you want to send.
    msg_area.send_keys(Keys.RETURN)

Enter fullscreen mode Exit fullscreen mode

Copy the code and paste it in your Python file with (.py) extension. Replace drivers path and WhatsApp contact in code. And then run it. It will show a WhatsApp web interface. Scan the QR code. And you are done.

Now you can change the code, according to your various requirements.

Top comments (13)

Collapse
 
david_lara_ba4856e407b609 profile image
David Lara

Great tutorial! Automating WhatsApp with Python and Selenium opens up a lot of interesting use cases. For anyone exploring more advanced features or looking to test automation with modded versions of WhatsApp that offer extended functionality, GetModsAPK is a helpful resource. Just make sure to run everything in a safe test environment, especially when working with unofficial builds. Looking forward to more automation content like this!

Collapse
 
david_lara_ba4856e407b609 profile image
David Lara

Awesome tutorial on automating WhatsApp with Python and Selenium! Just like automating tasks saves time and boosts efficiency, keeping track of your emirates id status ensures you manage important updates without delays.

Collapse
 
david_lara_ba4856e407b609 profile image
David Lara

Great guide! I’ve been exploring automation for NA5 WhatsApp, and while it's a modded version, the structure is similar enough that Selenium-based scripts still work with minor tweaks. It's a handy approach for testing bulk messaging or scheduling without relying on APIs. Anyone else tried automation with NA5 or similar mods?

Collapse
 
edem_jack_fb61387496ec3e7 profile image
Edem jack

Selenium is a great choice for WhatsApp automation, especially for tasks like sending messages in bulk whatsapp plus original. Just a heads-up—WhatsApp's policies may restrict automation, so it's good to use it responsibly to avoid potential bans. Also, keeping ChromeDriver updated helps prevent compatibility issues.

Collapse
 
swarnim_badholiya_071de73 profile image
Swarnim Badholiya

Awesome guide! Automating WhatsApp with Python and Selenium can be a game-changer for businesses looking to streamline messaging, notifications, and customer interactions. The ability to schedule messages and automate repetitive tasks saves time and improves efficiency.

At Ajackus, we specialize in automation solutions that enhance business workflows. If you're interested in more advanced automation strategies, check out our latest insights here: *ajackus.com/partner-platforms/what... *

Great work, @seikhchilli! Looking forward to more automation tutorials.

Collapse
 
david_lara_ba4856e407b609 profile image
David Lara • Edited

Great guide on WhatsApp automation! If you're also planning travels or need timely updates while working on such projects, marmaray saatleri can help you stay on top of Istanbul’s train schedules. Efficient time management is key!

Collapse
 
david_lara_ba4856e407b609 profile image
David Lara

Great work showing how automation can simplify repetitive tasks. At CGPACalcs.com, we apply the same principle to academics—automating things like 6.85 CGPA in percentage (which is 65.08%) or calculating scores with a Negative Marks Calculator. Accuracy and time-saving go hand in hand, whether it's code or class.

Collapse
 
emmalily profile image
Emma lily • Edited

This is a great tutorial for automating WhatsApp using Python and Selenium! It’s impressive how you can send messages programmatically, and the step-by-step instructions make it easy to follow. Just a reminder to use this responsibly and avoid spamming your contacts on WhatsApp. Automating tasks can save time, but it's essential to respect others' privacy. Thanks for sharing this code; I’m looking forward to customizing it for my needs!

Collapse
 
agokuzeusa profile image
Goku Zeas

WTPGold improves your messaging fm whatsapp v10.24 experience with a range of tweaks and additions, adding new features that surpass the app's default functionalities.

Collapse
 
raphaelvserafim profile image
Raphael Serafim

This is very good API WhatsApp

Some comments may only be visible to logged-in visitors. Sign in to view all comments.