This repository provides a Python script that automates sending WhatsApp messages via WhatsApp Web, using Selenium and webdriver-manager. It opens WhatsApp Web in a persistent Chrome session, allowing you to send predefined messages to any phone number in international format, without scanning a QR code each time.
- Uses an existing Chrome user profile to avoid repeated QR logins.
- Automatically handles the “Continue to Chat” button for new contacts.
- Sends messages to a specified phone number.
- Simple and straightforward Python code.
- Python 3.7+ installed
- Google Chrome browser installed
- A Chrome user profile that’s already logged into WhatsApp Web
- Clone or download this repository.
- Open a terminal (or command prompt) in the project directory.
- Install dependencies:
pip install selenium webdriver-manager -
Locate your Chrome user data folder. On Windows, it’s usually:
Update the path in the script if needed.
C:\Users\[YourUser]\AppData\Local\Google\Chrome\User Data\Default - Make sure your Chrome version matches the ChromeDriver installed by
webdriver-manager(this is usually handled automatically).
Open the Whatsapp_automated.py (or similar) file and adjust the following line to your own profile path:
CHROME_PROFILE_PATH = r"C:\Users\rokas\AppData\Local\Google\Chrome\User Data\Default"
Then, specify a valid phone number in international format (without +), and your desired message:
# Example usage at the bottom of the file:
if __name__ == "__main__":
test_number = "44XXXXXXXXXX" # Replace with a valid phone number
test_message = "Hello!" # Replace with your message
send_whatsapp_message(test_number, test_message)
Run the script:
python Whatsapp_automated.py
- The first time you run this script with a new Chrome user profile, WhatsApp Web may require you to scan the QR code. Once scanned, you remain logged in.
- For new contacts, WhatsApp shows a “Continue to Chat” link. The script attempts to click it automatically.
- If you encounter NoSuchElementException, it may be because WhatsApp Web’s UI changed. Update the XPaths or CSS selectors accordingly.
- Be mindful of WhatsApp’s terms of service and spam policies. Use responsibly.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from webdriver_manager.chrome import ChromeDriverManager import urllib.parse import timeCHROME_PROFILE_PATH = r"C:\Users\rokas\AppData\Local\Google\Chrome\User Data\Default" # Replace with your path
def send_whatsapp_message(phone_number, message): # Open Chrome and navigate to WhatsApp Web options = webdriver.ChromeOptions() options.add_argument(f"user-data-dir={CHROME_PROFILE_PATH}")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) try: # Make the WhatsApp URL with the phone number and message encoded_message = urllib.parse.quote(message) whatsapp_url = f"https://web.whatsapp.com/send?phone={{phone_number}}&text={{encoded_message}}" driver.get(whatsapp_url) # Wait for the 'Continue to Chat' button to appear if the contact is new and click it wait = WebDriverWait(driver, 10) time.sleep(3) # Pause to let the page load try: continue_button = wait.until( EC.element_to_be_clickable(( By.XPATH, "//a[contains(@href, 'web.whatsapp.com/send?phone=')]" )) ) continue_button.click() time.sleep(5) # let the chat load except: # If 'Continue to Chat' button does not appear, it's likely an existing contact pass # Find the Send button (data-icon='send') and click it send_button = wait.until( EC.element_to_be_clickable((By.XPATH, "//span[@data-icon='send']")) ) send_button.click() # Click the send button print(f"Message sent to {{phone_number}}!") time.sleep(3) except Exception as e: print(f"Error sending message to {{phone_number}}: {{e}}") finally: driver.quit() # Close the browser window
if name == "main": test_number = "44XXXXXXXXXX" # Replace with a valid phone number test_message = "Hello!" # Replace with your message send_whatsapp_message(test_number, test_message)
This project is provided “as is” without warranty of any kind. Use at your own risk and ensure you adhere to WhatsApp’s policies.
Happy Messaging!