Python Extras

WhatsApp Automation in Python

Have you ever wished to automatically wish your friends on their birthdays, or send a set of messages to your friend ( or any Whatsapp contact! ) automatically at a pre-set time, or send your friends by sending thousands of random texts on WhatsApp! Using Browser Automation you can do all of it and much more! 

First, you must install these:

1) Python Bindings for Selenium ( Browser Automation software ) 

pip install selenium

2) Chrome web driver 

Download Chrome driver ( choose your specific version ) Extract it in a known location, as we need the location later

3) Chromium Web Browser( Open-source version of chrome browser ) 

sudo apt-get install chromium-browser

That’s it! You are all set.

Let’s dive in right away

  • Python
from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byimport time # Replace below path with the absolute path# to chromedriver in your computerdriver = webdriver.Chrome('/home/saket/Downloads/chromedriver') driver.get("https://web.whatsapp.com/")wait = WebDriverWait(driver, 600) # Replace 'Friend's Name' with the name of your friend# or the name of a grouptarget = '"Friend\'s Name"' # Replace the below string with your own messagestring = "Message sent using Python!!!" x_arg = '//span[contains(@title,' + target + ')]'group_title = wait.until(EC.presence_of_element_located((    By.XPATH, x_arg)))group_title.click()inp_xpath = '//div[@class="_13NKt copyable-text selectable-text"][@data-tab="9"]'input_box = wait.until(EC.presence_of_element_located((    By.XPATH, inp_xpath)))for i in range(100):    input_box.send_keys(string + Keys.ENTER)    time.sleep(1)

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

Screenshot2

Then Run the script ( make sure that you have added the absolute path for the chrome driver and have replaced the target variable with your friend’s name ). Scan the QR code that appears on the screen and enjoy the power of python!

Screenshot3

Please use this script only for educational purposes, i am not responsible if your friends ( or even Whatsapp ) block you.

Feel free to modify the code. Try to : 

  1. Text Multiple Groups at once
  2. Send the messages from a predefined list of messages randomly or
  3. Send a completely random text.

Leave a Reply

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

Back to top button