【发布时间】: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