DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on

chat on whatsApp using python

Alt Text

How to send message to your friend on whatsapp using python.

if you had read some of my articles you would have learn how to send message on facebook Using python. won't you wish to use python to message on whatsapp too? and who doesn't want to do that? well there is a simple way just like that of facebook.
without wasting much of time let's go in to the lesson.

Firstly we need to install some libraries below :

  1. selenium
  2. webDriver_manager for chrome or geckodriver for firefox.

If you already have the two libraries mention don't bother.
After installation of the two libraries above then we can start writing the code.
If you had read my articles on how to message friends on facebook then just go straight
to the codes and copy otherwise read the following analysis.
Before we move to the code let me give you analysis on some function we are going to use in the program.

Some other requirements for the task

  • We need the selenium here to navigate to everywhere on the browser.
  • we will need the name of the friend or group that we want to send the message to just like it was stored on our phone contact.
  • your phone to scan the whatsapp on the browser.

How will you scan with your phone

It might be a little difficult to scan with your phone when you have a whatsapp web loaded on your browser if you had not been using your laptop to do whatsapp chat before so these are the step to take below.

  • go to web.whatsapp.com on your browser(in this case you don't need to go to this web site as your program as done that for you), you will see something like this below. Alt Text
  • Now go to your whatsapp on your phone.
  • click on the three dots at the right top of your screen.
  • click on 'WhatsApp Web' and you will see something like 'SCAN QR CODE' trying to scan something
  • Now set it to what you have on your laptop (like you want to camera) so that it scan your laptop
  • Your laptop should display your whatsapp now. ### Some functions needed
  • find_element_by_xpath() : A function provided by selenium module to find the element with css selector.
  • send_keys() : A function provided by selenium module also to write data or text into the box we use
  • webdriver.Chrome(): A function that will open new window of chrome
  • get() : A function we will used to open up the Facebook website
  • quit(): A function to close the browser when we are done.
  • sleep() : A function to delay the running of the script for some seconds.
  • input() and str : these are python function to prompt and convert to string respectively. you must have accustomed to these before.

Necessary modules to be import and important data

In our we ned to import the following from their lib.

  • webdriver from selenium.
  • sleep from time
  • ChromeDriverManager from webdriver_manager.chrome
  • Options from selenium.webdriver.chrome.options
  • Keys from selenium.webdriver.common.keys Also you will need to provide your friend name and message.

HERE IS THE CODE

from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(ChromeDriverManager().install())
# to chromedriver in your computer
message=str(input('enter your message')) # to get the message
driver.get("https://web.whatsapp.com/") #to open the whatsapp
sleep(39)
# here we need to scan with our phone
driver.find_element_by_xpath('//span[@title="Your friendName"][@dir="auto"]').click()# change that 'Your friendName to your own friend Name as it is written on your phone contact.

driver.find_element_by_xpath('//div[@dir="ltr"][@data-tab="6"][@spellcheck="true"]').send_keys(message, Keys.ENTER)
quit()
Enter fullscreen mode Exit fullscreen mode

wait for some minutes an see your message being sent. Did you find it interesting? that is just part of the power of Selenium.
the beautiful part of this is that you can write long text, repeating message Using loop.
and do different funny part. let's check the following codes.

Example

from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(ChromeDriverManager().install())

# to chromedriver in your computer
message=str(input('enter your message')) # to get the message
driver.get("https://web.whatsapp.com/") #to open the whatsapp
sleep(39)
# here we need to scan with our phone
driver.find_element_by_xpath('//span[@title="frndName"][@dir="auto"]').click()#your friend name to substitute that frndName
for msg in mess:
    driver.find_element_by_xpath('//div[@dir="ltr"][@data-tab="6"][@spellcheck="true"]').send_keys(msg, Keys.ENTER)
quit()
# This is where I entered the the message and Did click Enter
Enter fullscreen mode Exit fullscreen mode

Did you see how the message look like ? it send the message one by one character. try to write for loop for the message and manipulate your message the way you want, you can even sent blank message if you want.
enjoy coding! see you in the next lesson where we will be discussing instagram login with python. remember to like and comment below.

Top comments (0)