Send WhatsApp Message Automatic using python

Sonu Gupta
3 min readDec 20, 2020

Here is simple program or project that written in python language which can send whatsapp message using web whatsapp.

Now Python is used to do all things automatically so i try this one to send whatsapp message.

This is very simple and easy program to send multiple person at a time. for that i use two library of python

  1. selenium
  2. socket

Here selenium is used to open the browser and search the on google and socket is used to create and eastabilsh sequre connection to google.

List of import which i used to make program

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

Here i used sleep to wait some time to open the web browser and web whatsapp

# message you want to send
message_text = 'Hello Firefox'

This variable is used to send message.

# no. of time you want the message to be send
no_of_message = 1

Same message how many times you want to send

# list of phone number can be of any length
moblie_no_list = [91first_number,91second_number]

here you can all list of mobile number along with country code 91(india)

def element_presence(by, xpath, time):
element_present = EC.presence_of_element_located((By.XPATH, xpath))
WebDriverWait(driver, time).until(element_present)

This function is used to select the tags of web browser and wait for some time.

def is_connected():
try:
# connect to the host -- tells us if the host is actually
# reachable
socket.create_connection(("www.google.com", 80))
return True
except :
is_connected()

This function will check the connection with google

driver = webdriver.Firefox(executable_path='/.../geckodriver/geckodriver')
driver.get("http://web.whatsapp.com")
# wait time to scan the code in second
sleep(10)

Here i use the firefox for all browser different geckodriver we have to download and put here it’s path than it will be work.

def send_whatsapp_msg(phone_no, text):
driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
try:
driver.switch_to_alert().accept()
except Exception as e:
pass
try:
element_presence(By.XPATH, '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]', 30)
txt_box = driver.find_element(By.XPATH, '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
global no_of_message
for x in range(no_of_message):
txt_box.send_keys(text)
txt_box.send_keys("\n")
except Exception as e:
print("invailid phone no :" + str(phone_no))
for moblie_no in moblie_no_list:
try:
send_whatsapp_msg(moblie_no, message_text)
except Exception as e:
sleep(10)
is_connected()

This is the function to send the message on number which are listed in moblie_no_list list.

This is send Message screen shot

When you scan your QR code in browser automatically it will send the message to that number

This is the first number

This is first number message

This is second number screen shot

Second Number message

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

# message you want to send
message_text = 'Hello Firefox'

# no. of time you want the message to be send
no_of_message = 1

# list of phone number can be of any length
moblie_no_list = [91897992,91636681]


def element_presence(by, xpath, time):
element_present = EC.presence_of_element_located((By.XPATH, xpath))
WebDriverWait(driver, time).until(element_present)


def is_connected():
try:
# connect to the host -- tells us if the host is actually
# reachable
socket.create_connection(("www.google.com", 80))
return True
except :
is_connected()


driver = webdriver.Firefox(executable_path='/home/sonu/Desktop/MachineLearning/test_prog/geckodriver/geckodriver')
driver.get("http://web.whatsapp.com")
# wait time to scan the code in second
sleep(10)


def send_whatsapp_msg(phone_no, text):
driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
try:
driver.switch_to_alert().accept()
except Exception as e:
pass

try:
element_presence(By.XPATH, '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]', 30)
txt_box = driver.find_element(By.XPATH, '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
global no_of_message
for x in range(no_of_message):
txt_box.send_keys(text)
txt_box.send_keys("\n")
except Exception as e:
print("invailid phone no :" + str(phone_no))


for moblie_no in moblie_no_list:
try:
send_whatsapp_msg(moblie_no, message_text)

except Exception as e:
sleep(10)
is_connected()

This is the hole program you have to just download the driver as par browser and put path in execultable_path

Thans for reading this artical i hope you get some extra knowledge in python.

--

--

Sonu Gupta

I am a Python developer specialised in Machine Learning Data Science and backend development for complex scalable web apps using Django.