Reputation: 1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
#OpenWhatsAppWeb
driver = webdriver.Chrome()
driver.get("https://web.whatsapp.com/")
input("Scan QR code and press Enter...") #WaitforusertoscanQRcode
print("Logged in successfully.")
#Groupnameandmessage
group_name = "Young Thugs United"
message = "This is an automated message."
#Searchforthegroup
print("Searching for group...")
wait = WebDriverWait(driver, 10)
search_box = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@contenteditable='true']")))
search_box.send_keys(group_name)
time.sleep(2)
search_box.send_keys(Keys.ENTER)
print(f"Group '{group_name}' found. Sending messages every 15 seconds...")
#Continuousmessagesending
while True:
try:
# Wait for the message input box to appear
msg_box = driver.find_element(By.XPATH, "//div[@title='Type a message']")
#Typeandsendthemessage
msg_box.send_keys(message)
msg_box.send_keys(Keys.ENTER) # Press Enter to send
print("Message sent!")
#Waitfor15secondsbeforesendingthenextmessage
time.sleep(15)
except Exception as e:
print(f"An error occurred: {e}")
break #Exitthelooponerror
This a code for a python program to send automated messages without any inputs except the automated message that is to be sent.
After the program runs chrome window opens wherein the user has to scan the Whatsapp QR to give the program access to his account.
After which the program searches for the specified group on whatsapp search bar.
Now after this the group opens if and only if it has any unread messages.(Here the name of the group is Young Thugs)
After which it is supposed to send the automated message in the group.... THIS IS THE PART WHERE THE CODE IS THROWING ERROR...
#Continuousmessagesending
while True:
try:
# Wait for the message input box to appear
msg_box = driver.find_element(By.XPATH, "//div[@title='Type a message']")
THIS IS THE CHUNK OF CODE THAT IS RESPONSIBLE FOR THAT PART.
Upvotes: -2
Views: 34