首页 > 解决方案 > 如何使用 Python 解决自动化 WhatsApp 中的问题

问题描述

当我尝试搜索我的联系人时,我的代码在列表中出现错误。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
import time

class WhatsappBot:
    def __init__(self):
        # Parte 1 - A mensagem que você quer enviar
        self.mensagem = "Testando"
        # Parte 2 - Nome dos grupos ou pessoas a quem você deseja enviar a mensagem
        self.grupos_ou_pessoas = ["Mae","Matheus", ]
        self.filepath = input("Enter your filepath (images/video): ")
        self.teste = "Mae"
       
                        
        options = webdriver.ChromeOptions()
        options.add_argument('lang=pt-br')
        self.driver = webdriver.Chrome(
            executable_path=r'./chromedriver.exe', chrome_options=options)
             

    def EnviarMensagens(self):
        self.driver.get("https://web.whatsapp.com")
        time.sleep(60)
        
        for grupo_ou_pessoa in self.grupos_ou_pessoas:
            procurar = self.driver.find_element_by_class_name("_3xpD_")
            procurar.click()
            procurar.send_keys(self.grupos_ou_pessoas)
            time.sleep(3)
            campo_grupo = self.driver.find_element_by_xpath(
                f"//span[@title='{grupo_ou_pessoa}']")
            time.sleep(2)
            campo_grupo.click()
            attachment_box = self.driver.find_element_by_xpath("//span[@data-icon = 'clip']")
            attachment_box.click()
            image_box = self.driver.find_element_by_xpath("//input[@accept='image/*,video/mp4,video/3gpp,video/quicktime']")
            image_box.send_keys(self.filepath)
            time.sleep(2)
            chat_box = self.driver.find_element_by_class_name("_3FRCZ")
            time.sleep(3)
            chat_box.click()
            chat_box.send_keys(self.mensagem)
            botao_enviar = self.driver.find_element_by_xpath(
                "//span[@data-icon='send']")
            time.sleep(3)
            botao_enviar.click()
            time.sleep(3)


bot = WhatsappBot()
bot.EnviarMensagens()

我该如何解决?

标签: pythonlistauthentication

解决方案


推荐阅读