-1

I'm creating a bot for whatsapp, but I'm not able to search for the contact's name using the function find_element("xpath", ... )

My code:

from selenium import webdriver
import time


class WhatsappBot:
    def __init__(self):
        # Parte 1 - A mensagem que você quer enviar
        self.mensagem = "test"
        # Parte 2 - Nome dos grupos ou pessoas a quem você deseja enviar a mensagem
        self.grupos_ou_pessoas = ["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(30)
        for grupo_ou_pessoa in self.grupos_ou_pessoas:
            campo_grupo = self.driver.find_element("xpath",
                f"//span[@title='{grupo_ou_pessoa}']")
            time.sleep(3)
            campo_grupo.click()
            chat_box = self.driver.find_element(By.CLASS_NAME,'_13mgZ')
            time.sleep(3)
            chat_box.click()
            chat_box.send_keys(self.mensagem)
            botao_enviar = self.driver.find_element("xpath",
                "//span[@data-icon='send']")
            time.sleep(3)
            botao_enviar.click()
            time.sleep(5)


bot = WhatsappBot()
bot.EnviarMensagens()

The error

Robert
  • 7,394
  • 40
  • 45
  • 64
  • 1
    Please don't post images of text on StackOverflow; instead, just copy the error result and paste it as text inside a code block, as you did for your source code. – Conal Tuohy Mar 16 '23 at 02:39

1 Answers1

0

Have you tried defining XPath this way:

from selenium.webdriver.common.by import By

botao_enviar = self.driver.find_element(By.XPATH,"//span[@data-icon='send']")
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24