Whatsapp using Python

Whatsapp using Python

Whatsapp is one of the most popular applications in the world. Automating Whatsapp using python is a lot of fun. Sending messages automatically will be a nice idea.Well, then you are at the right place.Whatsapp using python is the work done by a library called Selenium which is famous for automation of browsers. Here’s your step by step guide to automating your WhatsApp using Python. Automation of WhatsApp is very easy and I will be explaining to you in detail about the program that I wrote, which allows Sending messages automatically at a pre-set time, or send your friends thousands of random text on WhatsApp! Using Browser Automation is a lot of fun.

First lets’ jump on to installation of Components for “Whatsapp using Python”:

  1. Python Should be there any version will work leaving don’t confuse in little syntax changes in new versions. I am using Python 3.6.1
  2. Python Selenium library(Browser Automation library)     
  3.  To install Selenium Library Type the command:  pip install selenium                                                                                                                                                                                              
  4. Chrome WebDriver
    Download Chrome driver from here: Chromedriver ( choose your specific version in accordance with your support) Extract it to a known location, as we need the location later to open the browser from specific place automatically so exact location is a must.
  5.  Type the command to install chromium driver :  sudo apt-get install chromium-browser

YOU ARE DOING WELL GUYS…… Half of the Job is Done!!!

Let’s Jump into the program from here:

Automation is very easy but understanding the program is very important. So take your time and understand it.
PLEASE TRY TO FOLLOW INDENTATION WHILE COPYING THIS CODE OR DOWNLOAD FROM GITHUB

CODE  >>

#Different Selenium library automation tools will be required 
 from selenium import webdriver
 from selenium.webdriver.support.ui import WebDriverWait
 from selenium.webdriver.support import expected_conditions as EC
 from selenium.webdriver.common.keys import Keys
 from selenium.webdriver.common.by import By
 import time
 #Now you need to replace the location inside webdrive.chrome() 
 #with your absolute location of chrome driver 
 #As soon this statement gets your browser opens automatically
 #r''' ''' is given to remove special characters error 
 #if you dont use this because of '\' there will be an error
 # there are different ways of doing this 
 driver = webdriver.Chrome(r'''C:\Users\ANURAG\Desktop\chromedriver''') 
 #After opening browser open web.whatsapp.com through next command
 driver.get("https://web.whatsapp.com/")
 #Now you need to scan the QR CODE on browser through your mobile whatsapp
 wait = WebDriverWait(driver, 600) 
 #Now Choose the Person's Name Whom you want to send message it should be exactly same as whatsapp name 
 target = '"Ajeet"'
 #Write Message which you want to send
 string = "Ajeet it's party time!!!"
 #By this you will give the location where to search your target or contact
 #So it will specify the place of message box on top 
 #and than search inside that your contact name if found than move ahead
 x_arg = '//span[contains(@title,' + target + ')]'
 #By.XPATH specifies a specific attribute in browser for example your message box
 #wait.until() specifies that wait until your condition is found
 group_title = wait.until(EC.presence_of_element_located((By.XPATH, x_arg)))
 #Now we perform the click task of the group_title to check every condition
 group_title.click()
 #Here input text xpath should be given for your input text message box
 inp_xpath = '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'
 #This statement will perform the same task of waiting until your condition is satisfied
 input_box = wait.until(EC.presence_of_element_located((By.XPATH, inp_xpath)))
 #Now for loop does the work of sending your content how many times you want to repeat
 for i in range(100):
      #.send_keys is used to enter your string message inside input box and send it
      #By pressing Keys.Enter
      input_box.send_keys(string + Keys.ENTER)
     #sleep is used to take some time for send that is given 1 millisecond
      time.sleep(1)

#Program successful You can find the source code from GitHub 
#Support writer by sharing this article with your friends

MOBILE is must and you should have a Whatsapp account it is not anonymous work guys.

Keep your mobile phone with you. Choose Whatsapp web from the top bar in Whatsapp (3 dots)

web whatsapp with qr code

Run the script  (make sure that you have added the absolute path for chromedriver and have replaced target variable with your friend’s name ). Scan the QR code that appears on the screen and get amazed.

open whatsap web through phone                                          scan the qr code

 

Feel Free to modify the code and use it for Educational purposes. Try Your innovative ideas all you programmer’s out there…..

Selenium can be used to modify all the browsers including the most famous browsers like chrome and firefox

NOTE : Also read Whatsapp Live Location

Later on, we will be combining the code with a birthday reminder application in the later half so all of you out there who have a tough time remembering your friend’s or girlfriend’s birthday Stay tuned.

Check Out a Sample Video Of how to do it

Feel free to leave a comment down below if you like this article and topic’s on which you want articles.

About Anurag

You can call me the Tech Buffer because I love the way technology is moving inside me. I fascinate the way phones take pictures to the way Google finds its search result.

View all posts by Anurag →

5 Comments on “Whatsapp using Python”

  1. The 5th point isn’t working for me. I used the below command in my windows cmd interface.
    sudo apt-get install chromium-browser
    ‘sudo’ is not recognized as an internal or external command,

    1. If you are making this on windows than no need to install chromium driver it is already in .exe file just you need to extract it and you should know the location where you extracted it.That command is useful for Ubuntu user to install chromium drivers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.