WhatsApp Web自动化与Selenium不工作

2024-06-01 20:56:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现this python script on github可以通过Selenium自动发送WhatsApp Web消息。

#https://www.github.com/iamnosa
#Let's import the Selenium package
from selenium import webdriver

#Let's use Firefox as our browser
web = webdriver.Firefox()
web.get('http://web.whatsapp.com')
input()

#Replace Mr Kelvin with the name of your friend to spam
elem = web.find_element_by_xpath('//span[contains(text(),"Mr Kelvin")]')
elem.click()
elem1 = web.find_elements_by_class_name('input')
while True:
    elem1[1].send_keys('hahahahahahaha')
web.find_element_by_class_name('send-container').click()

虽然这是为了垃圾邮件,我试图调整它的一个好的目的,但脚本,它的立场似乎不起作用。它不通过WhatsApp网络发送消息,只需加载一个QR认证屏幕,然后在我用手机进行认证后就什么也不做了。

有什么线索说明为什么会发生这种事吗?我在Firefox上运行最新版本的Selenium WebDriver,geckodriver已经解压到/usr/bin/。


Tags: thenameimportgithubcomweb消息input
3条回答

我实现二维码扫描的方法是通过检测页面上是否存在搜索栏。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


chatlist_search = ".jN-F5.copyable-text.selectable-text"

web.get("https://web.whatsapp.com")
WebDriverWait(web, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, chatlist_search)))

这将等到聊天搜索栏呈现在页面上,否则将在60秒后超时。

我意识到这篇文章已经过时了,但它似乎仍然经常被人关注。 对@vhad01的按键解释是有道理的,但对我不起作用。

一个简单的肮脏的解决方法对我有效: 将input()替换为

import time
time.sleep(25)

25是等待代码进一步执行的秒数。(15也应足以扫描二维码,…)。

这一行:

input()

正在等待按键继续。 扫描后按任意键即可。

相关问题 更多 >