WhatsApp using Python
Have you ever wished to automatically wish your friends on their birthdays, or send a group of messages to your friend ( or any Whastapp contact! ) automatically at a pre-set period, or send your friends by sending thousands of random text on WhatsApp! Using Browser Automation you'll do all of it and far more!
First of all, you should install the following things:
1.WebDriver for Chrome Browser
WebDriver is an open-source tool for automated testing of web apps across many browsers. It provides capabilities for navigating to sites, user input, JavaScript execution, and more. ChromeDriver may be a standalone server that implements the W3C WebDriver standard. ChromeDriver is out there for Chrome on Android and Chrome on Desktop (Mac, Linux, Windows and ChromeOS).
If you are using windows operating system then you have to download WebDriver for Windows.
2.Installation of Selenium WebDriver
For our test project, we'll use Selenium WebDriver’s Python bindings with Google Chrome and ChromeDriver. We could use any browser, but let’s use Chrome because (a) it's a really high market share and (b) its Developer Tools will are available handy later.
After downloading the WebDriver for Chrome using the download button provided. Then, you have to extract the file. After extracting the file you will find an application called chromedriver.exe. Run that application and installed it.
Verify that ChromeDriver works from the command line:
$ chromedriver
Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
You can download the Python bindings for Selenium from the PyPI page for selenium package. However, a far better approach would be to use pip to put in the selenium package. Python 3.6 has pip available within the standard library. Using pip, you'll install selenium like this:
pip install seleniumFor Python 3 you can install given as below.
pip3 install selenium
That's it! Now you can automate Whatsapp web using selenium and python using following steps.
Here is the Code:
from selenium import webdriver
driver = webdriver.Chrome('/home/saket/Downloads/chromedriver')
driver.get("https://web.whatsapp.com/")
target = '"Friend\'s Name"'
string = "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="input"][@dir="auto"][@data-tab="1"]'
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 with you. Choose the WhatsApp web from the highest bar in WhatsApp(3 dots).
Connect your mobile with web.whatsapp.com by scanning QR code which scans in your mobile.
Then run the script ( confirm that you simply have added absolutely the path for chrome driver and have replaced target variable together with your friend's name ). Scan the QR code that appears on the screen and luxuriate in the facility of python!
Please use this script only for educational purposes, i am not responsible if your friends ( or even Whatsapp ) block you.
For Practise purpose you can watch the following video which helps you a lot.
0 Comments