Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Whatsapp using Python?
WhatsApp automation using Python allows developers to send messages programmatically. Since WhatsApp doesn't provide official API access for personal accounts, we use Selenium to automate WhatsApp Web through a browser interface.
Requirements
To create a WhatsApp automation script, you need three essential components ?
Install Selenium
Install Selenium using pip ?
pip install selenium
Chrome WebDriver
Download the Chrome WebDriver compatible with your Chrome browser version from the official ChromeDriver website. Extract it to a known location on your system.
WhatsApp Account
Ensure you have an active WhatsApp account linked to your mobile device for QR code scanning.
Sending WhatsApp Messages
Here's a complete script to send WhatsApp messages to specific contacts ?
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
import sys
# Replace with the absolute path to chromedriver on your system
driver = webdriver.Chrome(r'c:\users\rajesh\Desktop\chromedriver')
# Open WhatsApp Web
driver.get("https://web.whatsapp.com/")
# Wait for user to scan QR code (10 minutes timeout)
wait = WebDriverWait(driver, 600)
# Replace 'My Contact' with your target contact name
target = '"My Contact"'
# Message to send (can be passed as command line argument)
message_text = "Hello from Python!"
if len(sys.argv) > 1:
message_text = sys.argv[1]
# Find the contact
x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located((
By.XPATH, x_arg)))
print("Contact found, clicking...")
group_title.click()
# Find the message input box and send message
message_box = driver.find_element(By.XPATH,
'//div[@contenteditable="true"][@data-tab="10"]')
message_box.send_keys(message_text)
# Click send button
send_button = driver.find_element(By.XPATH,
'//span[@data-icon="send"]')
send_button.click()
print("Message sent successfully!")
time.sleep(2)
driver.close()
How to Use
Run the script from command prompt with your message ?
python whatsapp_automation.py "Hello from Python!"
QR Code Scanning Process
When you run the script, Chrome will open WhatsApp Web. You'll see a QR code on the screen. Open WhatsApp on your mobile device, go to "WhatsApp Web" from the menu, and scan the QR code to authenticate.
Important Considerations
- XPath Updates: WhatsApp Web frequently updates its interface, so XPath selectors may need adjustment
- Rate Limiting: Avoid sending too many messages quickly to prevent account restrictions
- Authentication: You'll need to scan the QR code each time unless you save the session
- Contact Names: Ensure the contact name exactly matches what appears in WhatsApp
Conclusion
WhatsApp automation with Python using Selenium provides a way to send messages programmatically. Remember to use this responsibly and comply with WhatsApp's terms of service to avoid account suspension.
