Python Selenium不起作用的WhatsApp Web自动化

Python Selenium不起作用的WhatsApp Web自动化,python,selenium,whatsapp,Python,Selenium,Whatsapp,我发现它通过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 Ke

我发现它通过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/

这一行:

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

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

对我来说,一个简单的肮脏的解决办法: 将
input()
替换为

import time
time.sleep(25)

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

我在写selenium脚本来安排我的MSG,我遇到了你的问题。是的,问题是输入()行。 不使用input():

使用time.sleep(),无疑它会起作用,但最好使用隐式等待(15)

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

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

我使用了一种更不同的方法来whatsapp_login()和QR扫描。要查看我的回购链接:


您也希望使用这种方法。

更好的方法是扫描命令行中的QR代码点击返回,然后继续代码

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

这就是您所需要的,并且应用于此问题的逻辑也不是很模糊。

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

通过导入从selenium.webdriver.common.by导入 从selenium.webdriver.support.ui导入WebDriverWait 从selenium.webdriver.support将预期的_条件导入为EC chatlist_search=“.jN-F5.可复制文本.可选择文本” web.get(“https://web.whatsapp.com") WebDriverWait(web,60).until(EC.visibility_of_element_located((By.CSS_SELECTOR,chatlist_search))) 这将等待聊天搜索栏显示在页面上,否则将在60秒后超时