Problems in automating sending messages on WhatsApp through Selenium library by Python

Greetings! I am with a project of automatic sending of messages via WhatsApp that consists of sending messages provided with special characters (emojis and other symbols) saved in files .txt, however, whenever I use the following code:

Trava = open('c:/Users/Marco Neto/Desktop/ataquepalhaco.txt', encoding='utf-8').read()

(...)

self.driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')

It gives the following error:

selenium.common.exceptions.WebDriverException: Message: unknown error: ChromeDriver only supports characters in the BMP

I have tried to research about, however, the only solution I found regarding this problem was to switch from webdriver and instead of using Chrome's, use Firefox's. I tried through the following code but that unfortunately also went wrong:

self.driver = webdriver.Firefox(executable_path=r'./geckodriver.exe')

Error of the above code:

selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

In short, I would like an alternative or resolution to the codes so that I can insert special characters into a file .txt and send it by WhatsApp by selenium. Grateful for the attention.

Author: Scharlachrot, 2020-07-11

2 answers

Cannot send emojis using Chrome at this time due to a bug . You should use firefox.

As for the error in starting Firefox, there are several possibilities:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
browser = webdriver.Firefox(capabilities=cap, executable_path="C:\\path\\to\\geckodriver.exe")
browser.get('http://google.com/')
browser.quit()

Also, the executable_path parameter of webdriver.Firefox expects a full path, but you passed a relative one. Try replacing'./ geckodriver.exe ' by the full address of where this executable is.

 0
Author: felubra, 2020-07-12 12:04:09

I have already had a project of this feature in hand.

I used the WhatsApp api to automate the sending of texts and emojis, sending photos and videos was sooooo loooongo the time coding but it worked well.

To send message using pc, you initially have to go through QR Code. Da to circumvent this mechanism using mv.

Unfortunately in your doubt did not pass the code,only a part but I will show a simple example of my github.

When started you will ask for authentication by mobile phone, will be redirected to the message Page and send text to each number indicated.

from selenium.webdriver.common.by import By
from selenium import webdriver
import schedule
 
class Whatsapp:

    def driver(self):
        driver = webdriver.Chrome('/home/..') # driver google
        return driver

    def mensagem_whatsapp(self, mensagem, numeros_celulares):

       driver_extensao = Whatsapp().driver()
        driver_extensao.get(f"https://web.whatsapp.com/send?phone={numeros_celulares}&source=&data=#") # api web
        print("Autentificaçao[Web, Desktop] ")
        driver_extensao.find_element(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
        campo = driver_extensao.find_element(By.XPATH, '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
        print("Envio : ", campo.send_keys(mensagem))
        campo.send_keys("\n")


def robo():
    pessoas = [0010101] # lista de numeros
    mensagemE = "Ola, estao" # frase, pode interagir com teu txt

    enviarAgora = Whatsapp()
    for possomandar in pessoas:
        enviarAgora.mensagem_whatsapp(mensagem=mensagemE, numeros_celulares=pessoas)

#only start with Main with the robot function

Whatsapp font

 0
Author: jeferson.cardoso, 2021-02-03 17:58:37