How to Automate WhatsApp Messaging Using Selenium and CSV Files
Automating repetitive tasks can save time and reduce errors. In this tutorial, we’ll demonstrate how to send WhatsApp messages in bulk
using Python, Selenium, and a CSV file. Follow the steps below to build and run this automation script.
Step 1: Install Required Dependencies
Before starting, ensure you have Python installed on your system. Then, install the required libraries using pip:
pip install selenium
Also, download the Chrome WebDriver that matches your browser version from here and add it to your system’s PATH.
Step 2: Prepare Your CSV File
Create a CSV file named contacts.csv
in the same directory as your script. This file should contain two columns:
- Phone Number: The recipient’s phone number (with country code, e.g., 911234567890 for India).
- Message: The message text to be sent.
Example content of contacts.csv
:
1 2 3 |
phone,msg 911234567890,Hello, this is a test message. 911987654321,Happy New Year! Wishing you all the best. |
Step 3: Write the Python Script
Below is the complete Python script to send WhatsApp messages:
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time import csv # Initialize the Chrome driver driver = webdriver.Chrome() # Open WhatsApp Web baseurl = "https://web.whatsapp.com" driver.get(baseurl) # Wait for QR Code scan (increase sleep time if needed) print("Please scan the QR code to log in to WhatsApp Web.") time.sleep(10) # Read contacts and messages from the CSV file with open('contacts.csv', newline='') as csvfile: readContacts = csv.reader(csvfile) next(readContacts) # Skip the header row for phone, msg in readContacts: phonnum = phone message = msg # Construct the WhatsApp URL for the contact sameTab = (baseurl + '/send?phone=' + str(phonnum)) driver.get(sameTab) # Wait for the chat to load (increase sleep time if necessary) time.sleep(8) # Find the message input field and send the message content = driver.switch_to.active_element content.send_keys(message) content.send_keys(Keys.RETURN) # Add a delay to avoid potential errors due to quick navigation time.sleep(8) # Close the browser driver.quit() |
Step 4: Run the Script
- Save the script as
whatsapp_automation.py
in the same directory as yourcontacts.csv
file. - Open a terminal or command prompt and run the script:
python whatsapp_automation.py
- When the script opens WhatsApp Web, scan the QR code with your mobile WhatsApp app.
- The script will send messages to the contacts listed in
contacts.csv
one by one.
Step 5: Adjust Sleep Timings
If your internet connection is slow or WhatsApp Web takes longer to load, increase the sleep times (time.sleep()
) in the script. This ensures that messages are sent correctly without interruptions.
Best Practices
- Respect Privacy: Use this script responsibly and ensure you have consent to message the recipients.
- Rate Limiting: Avoid sending messages too quickly to prevent being flagged by WhatsApp.
- Backup Your CSV File: Keep a backup of your
contacts.csv
file to avoid accidental data loss.