DEV Community

Raunak Tamang
Raunak Tamang

Posted on • Updated on

automate WhatsApp messages using Python

Project Title
Automate WhatsApp for sending messages to any particular person or group in WhatsApp.

Getting Started
It's not totally automated as the QR scan should be scanned manually. Everything is working fine with code as you soon run the code chrome will pop up and open WhatsApp web and scan the QR code from your phone. then type the contact name in the console box and the message you want to send and the number of times you wanna send.

prerequisites
Selenium Library

Chrome driver

installing

install selenium

python3 -m pip install selenium

Download and install chromedriver

download chrome driver here
code :

import web driver from selenium

from selenium import webdriver

creating an obect to open chrome and set the path inside () of chrome driver

driver = webdriver.Chrome('/Users/rauna/Downloads/chromedriver')

passing the WhatsApp url

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

getting data from user

name = input('enter the name of user')
msg = input('enter your message')
count = int(input("enter the count"))
input('enter after scanning QR code')

saving the defined contact name from your WhatsApp chat in user variable

user = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))

user.click()

name of span class of contatct

msg_box = driver.find_element_by_class_name('_13mgZ')

for i in range(count):
msg_box.send_keys(msg)
button = driver.find_element_by_class_name('_3M-N-')
button.click()

snippet from above code:
user = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))

you can see why [@title = "{}"] because user providing the name of the contact person you wanna send the message screenshot
Alt Text

msg_box = driver.find_element_by_class_name('_13mgZ')

('_13mgZ') is the class name of typing box you can see the image below

screenshot
Alt Text

button = driver.find_element_by_class_name('_3M-N-')

('_3M-N-') is the class name of send button as you can see in image below

screenshot
Alt Text

Output :
screenshot
Alt Text

screenshot
Alt Text

check out GitHub for a better understanding link is here:
https://github.com/cyber-hoax/automatewhatsapp

python #selenium #automation #WhatsApp

Top comments (9)

Collapse
 
muhimen123 profile image
Muhimen

This thing bothers me a lot too. I have good news and bad news for you.

The good news is, twillo has an API for WhatsApp.

The bad news is, you need to pay for this service.

So, I didn't bother messing around with the API. 😓

Collapse
 
cyberhoax profile image
Raunak Tamang

yeah i recently post a blog with twilio too it would pleasure if you can look into it :)
here's the link dev.to/cyberhoax/send-and-receive-...

Collapse
 
cyberhoax profile image
Raunak Tamang

I can feel that bro!

Collapse
 
maxsanwal profile image
Seemant Singh

pypi.org/project/whatsappchatbot/ have tried to create a chatbot .. check it out if you're interested

Collapse
 
cyberhoax profile image
Raunak Tamang

sure i will :)

Collapse
 
cyberhoax profile image
Raunak Tamang

now the official WhatsApp Business API is avaiable.

Collapse
 
luiz0x29a profile image
Real AI

still pricey as hell and too much bureaucratic

Thread Thread
 
cyberhoax profile image
Raunak Tamang

Couldn't agree more 😅

Collapse
 
cyberhoax profile image
Raunak Tamang

totally agree !