【问题标题】:Python webdriver seleniumPython webdriver 硒
【发布时间】:2020-06-20 01:33:59
【问题描述】:

我正在制作一个 whatsapp 机器人,我需要知道一种将其置于循环中的方法,直到有新消息到达以对其进行响应。

有什么想法吗?

我的代码:

from selenium import webdriver
import time

class WhatsappBot:
    def __init__(self):
        self.mensagem = "Mensagem teste" #mensagem a enviar
        self.grupos = ["Bloco de notas"] #grupos/user a enviar msg
        options = webdriver.ChromeOptions()
        options.add_argument('lang=pt-br')
        options.add_argument('--user-data-dir=/Users/arthurgomes/AppData/Local/Google/Chrome/User Data/Default')
        options.add_argument('--profile-directory=Default')
        self.driver = webdriver.Chrome(executable_path=r'./chromedriver.exe', options=options)

    def EnviarMensagens(self): 
        # <span dir="auto" title="Bloco de notas" class="_3ko75 _5h6Y_ _3Whw5">Bloco de notas</span>
        # <div tabindex="-1" class="_3uMse">
        # <span data-icon="send" class="">
        print('dhuawdhauw')
        self.driver.get('https://web.whatsapp.com/')
        time.sleep(5)
        count = 0
        while count < 3:  
            for grupo in self.grupos:
                grupo = self.driver.find_element_by_xpath(f"//span[@title='{grupo}']")
                time.sleep(0.2)
                grupo.click()
                chat_box = self.driver.find_element_by_class_name('_3uMse')
                time.sleep(0.2)
                chat_box.click()
                chat_box.send_keys(self.mensagem)
                botao_enviar = self.driver.find_element_by_xpath("//span[@data-icon='send']")
                time.sleep(0.2)
                botao_enviar.click()
                time.sleep(0.2)
                print(count)
                count +=1
        if count >= 3:
            exit()


bot = WhatsappBot()
bot.EnviarMensagens()

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver webdriver


    【解决方案1】:

    尝试检查要更改的聊天窗口 DOM 元素的来源。这段代码我没有测试过,它会导致程序进入死循环。

    import time
    timeout = 10 # check every 10 seconds for messages
    chatbox_xpath = "//div[@class='chatbox']" # representation only
    
    while True:
        source = driver.find_element_by_xpath(chatbox_xpath).get_attribute('innerHTML')
        time.sleep(timeout)
        if source != 
            driver.find_element_by_xpath(chatbox_xpath).get_attribute('innerHTML'):
           break
    

    【讨论】:

    • @AngelTm_ tbh 我真的不知道发生了什么,因为你的变量名不是英文的,而且我也无法从 xpath 推断出你在做什么。也许它会进入你那里的while循环。本质上,您需要弄清楚这一点,因为它是您的机器人并且您正在对其功能进行编程。你的问题一开始就很模糊,我推荐你How to Ask 了解一些提问的技巧。
    • 我能以某种方式与您联系并更好地解释吗?我真的需要这个xd
    • while count &lt; 3: 应替换为 while True 并且您在 while 循环 group_chat_src = [] 之外定义了某种列表,然后您 for 循环遍历这些组。每次将源代码更新为group_chat_src[grupo] = driver.find_element_by_xpath(chatbox_xpath).get_attribute('innerHTML'),然后在循环内部if group_chat_src[grupo] != driver.find_element_by_xpath(chatbox_xpath).get_attribute('innerHTML')# do stuff 否则pass。现在您需要 while 循环的退出条件。这就是我能做的一切。
    猜你喜欢
    • 2020-06-24
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2016-03-20
    • 2016-04-23
    • 1970-01-01
    相关资源
    最近更新 更多