pythonselenium-webdriverselenium-chromedriverwhatsapp

How to send automated message in whatsapp desktop


I was looking to have some fun and was annoying my friend by spam tagging him in a group chat . Then it hit me , why could i not write a for loop to spam his name as many times as i want .

I went to google and ChatGPT and it spat me out this

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time

# Path to Brave Browser executable
brave_path = "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"

# Path to ChromeDriver (if moved to /usr/local/bin, no need for full path)
chromedriver_path = "/usr/local/bin/chromedriver"

# Configure Selenium to use Brave
options = webdriver.ChromeOptions()
options.binary_location = brave_path

# Start WebDriver with Brave
driver = webdriver.Chrome(service=Service(chromedriver_path), options=options)

# Open WhatsApp Web
driver.get("https://web.whatsapp.com")

# Wait for login
input("Log in to WhatsApp Web and press Enter...")

# Example: Print the page title
print("Page title is:", driver.title)

# Wait for the search box to be available
wait = WebDriverWait(driver, 30)  # Wait for up to 30 seconds
search_box = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@contenteditable="true"][@data-tab="3"]')))

# Click on the search box and type the chat name
search_box.click()
search_box.send_keys("Group Name")  # Replace with your group's name
search_box.send_keys(Keys.ENTER)

# Wait for the message box to be both present and visible
message_box = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@contenteditable="true"][@data-tab="1"]')))
time.sleep(2)  # Optional: Add delay to ensure the message box is ready

# Initialize ActionChains
actions = ActionChains(driver)

# Send "@John" 100 times in the message box
for _ in range(100):
    actions.move_to_element(message_box).click().send_keys("@John").perform()
    actions.send_keys(Keys.SHIFT + Keys.ENTER).perform()  # To create a new line without sending

# Finally, send the message
actions.send_keys(Keys.ENTER).perform()

# Close browser
driver.quit()


i have literally 0 clue about selenium and i do know a little about python. Just for the kicks can anyone help me . also i have setup chromium and everything on my machine and was able to login and navigate to the group chat properly just the script wasnt writing his name 100 times and sending it to him

also itll be a great help if the script would tag him a 100 times if possible . also we were gonna play CS-Go

Just for fun if anyone lurks around here, do share a help. Thanks .


Solution

  • If using selenium library is not you priority, you can use a pyautogui library to do that. To install it, just type in the terminal

    pip install pyautogui

    Here is an example application. If you are going to open your whatsapp group and select a box where you send messages in 10 seconds, an application is going to send 100 messages. Put as a message variable whatever you want to spam your friends with, and here you go!

    import pyautogui, time
    
    time.sleep(10) # 10 seconds delay so you can open whatsapp and your chat, and select a message box.
    
    message = "Here you put any string you want to spam you friend with"
    for i in range(100): # Do this 100 times
        pyautogui.typewrite(message) # Writes a message into the whatsapp box
        pyautogui.typewrite(["enter"]) # Sends the message
    

    If you want to customize your code, here is the documentation of this library.