Selenium的WhatsApp Web自动化不起作用



我在github上发现了这个python脚本,它通过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网络发送消息,而是简单地加载一个二维码验证屏幕,然后在我用手机验证后它什么也不做。

关于为什么会发生这种情况,有什么线索吗?我正在Firefox上运行Selenium WebDriver的最新版本,geckodriver已经被提取到/usr/bin/中。

我意识到这篇文章比较老,但它似乎仍然经常被浏览。@vhad01的击键解释很有道理,但对我不起作用。

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

import time
time.sleep(25)

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

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

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秒后超时。

此行:

input()

正在等待按键继续。扫描后只需按任意键。

我正在写一个硒脚本来安排我的消息,我遇到了你的问题。是的,问题是input()行。不使用input():

使用time.sleep(),毫无疑问它会起作用,但最好使用implicit_wait(15)

Time.sleep()即使在扫描后也会让您等待。脚本完全停止,直到指定的秒。

在implicit_wait()中,if元素在指定时间之前出现,脚本将开始执行,否则脚本将抛出NoSuchElementException。

我使用了一种与whatsapp_log()和QR扫描更不同的方法。要查看我的回购链接:https://github.com/shauryauppal/PyWhatsapp

你也会喜欢这种方法。

更好的方法是扫描命令行中的二维码点击返回,然后对代码进行进一步处理。

browser = webdriver.Firefox()
browser.get('https://web.whatsapp.com/')
print('Please Scan the QR Code and press enter')
input()

这就是你所需要的,也不是很模糊的逻辑来应用于这个问题。

相关内容

最新更新