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.
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:
- firefox is not installed on your system, or is not in path
- 64-bit system, but Geckdriver / Firefox 32-bit; or vice versa
- on Windows, the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017, and 2019 are not installed-required for Geckodriver
- GeckoDriver for Firefox version 46 or earlier should be started as follows in modern versions of Selenium:
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.
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