/  Technology   /  Whatsapp using Python!

Whatsapp using Python!

Developer’s can automate the browser’s activity with a very smart package in python called Selenium. We can make use of whatsapp-web through the browser by using this.




We require three basic things to get things fulfill: Selenium.

Install selenium using pip by running the below command on your terminal −


$pip install selenium


  • Chrome/firefox or any other webdriver.

As I using chrome webdriver, below is a link to download chrome webdriver as per your os.

Create Whatsapp account.


Below code is an example to send whatsapp message using python to specific contacts.




from selenium importwebdriver
from selenium.webdriver.support.uiimportWebDriverWait 
from selenium.webdriver.supportimportexpected_conditions as EC 
from selenium.webdriver.common.keysimportKeys 
from selenium.webdriver.common.byimportBy 
import time 
import sys
#Replace below path with the absolute path of the \ 
#chromedriver in your computer 
driver =webdriver.Chrome(r'c:\users\rajesh\Desktop\chromedriver') 
wait =WebDriverWait(driver,600) 
#Replace 'My Bsnl' with the name of your friend or group name 
target ='"My Bsnl"' 
#Replace the below string with your own message 
x_arg='//span[contains(@title,'+ target +')]' 
print("Wait for few seconds") 
message =driver.find_elements_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')[0] 


Run the above scripts on command prompt by passing message as argument to the whatsapp contact−


>python whatsppPython.py "Hello" 

DevTools listening on 
<selenium.webdriver.remote.webelement.WebElement(session="83e7034b9a6f6b49e9e422e655f270d3", element="0.30994636046479007-1")> 
after wait 


As shown below the chrome browser will open with screen something like −


Whatsapp using Python



On your mobile device, Choose whatsapp web from the top bar in whatsapp. On the screen, scan the QR code that appears.

There we can see the message is send to a specific contact (“My Bsnl”) in our case.


Whatsapp using Python

Leave a comment