Send WhatsApp Message Automatic using python

  1. selenium
  2. socket
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 = [91first_number,91second_number]
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='/.../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 send Message screen shot
This is the first number
This is second number screen shot
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()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store