【问题标题】:WhatsApp Web automation with Selenium not workingSelenium 的 WhatsApp Web 自动化无法正常工作
【发布时间】:2017-06-22 03:56:37
【问题描述】:

我发现 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 Web 发送消息,而是简单地加载一个 QR 身份验证屏幕,然后在我使用手机进行身份验证后它什么也不做。

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

【问题讨论】:

    标签: python selenium whatsapp


    【解决方案1】:

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

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

    import time
    time.sleep(25)
    

    而 25 是等待代码进一步执行的秒数。 (15应该也够扫描二维码了,...)。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        这一行:

        input()
        

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

        【讨论】:

          【解决方案4】:

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

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

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

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

          我对 whatsapp_login() 和 QR 扫描使用了更不同的方法。看到我的回购链接:https://github.com/shauryauppal/PyWhatsapp

          你也喜欢这种方法。

          【讨论】:

            【解决方案5】:

            更好的方法是在命令行中扫描二维码点击返回,然后继续处理您的代码。

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

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

            【讨论】:

              猜你喜欢
              • 2020-06-21
              • 1970-01-01
              • 2015-09-05
              • 1970-01-01
              • 2020-03-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多