如何在whatsapp桌面上发送自动消息

问题描述 投票:0回答:1

我想找点乐子,却在群聊中给我的朋友发了垃圾邮件,惹恼了他。然后我突然想到,为什么我不能编写一个 for 循环来多次发送他的名字。

我去了谷歌和ChatGPT,它把我吐了出来

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()


我对 selenium 几乎一无所知,但我对 python 确实了解一点。只是为了好玩,任何人都可以帮助我。 我还设置了铬和我的机器上的所有东西,并且能够正确登录并导航到群聊 只是剧本没有写他的名字100遍并发送给他

如果可能的话,如果脚本能给他贴上 100 次标签,那也会有很大的帮助。 我们还要玩CS-Go

只是为了好玩,如果有人潜伏在这里,请分享帮助。 谢谢。

python selenium-webdriver selenium-chromedriver whatsapp
1个回答
0
投票

如果使用 selenium 库不是您的首要任务,您可以使用

pyautogui
库来做到这一点。要安装它,只需在终端中输入

pip 安装 pyautogui

这是一个示例应用程序。如果您要打开您的 Whatsapp 群组并选择一个在 10 秒内发送消息的框,则应用程序将发送 100 条消息。将您想要向朋友发送垃圾邮件的任何内容作为消息变量,然后就可以了!

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

如果您想自定义代码,这里是该库的文档。

© www.soinside.com 2019 - 2024. All rights reserved.