-2

Sorry for the lack of information, I promise to read the posting rules.

The script is based to send messages in whatsapp groups.

The script opens the browser on the whatsapp web page and then I read the QR CODE, for the script to trigger the messages.

The problem is that it sends a message only once to the list of groups, I need it to send a message per hour to each group, without opening the browser, because if this is done, I will need to read the QR CODE again and that cannot happen.

Version Selenium 4.0

Complete code, it is without errors, I would just like to implement the retry action without opening the browser again.

Comments

import pandas as pd
import xlsxwriter 
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ChromeOptions, Chrome
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

opts = ChromeOptions()
opts.add_experimental_option('detach', True)
opts.add_experimental_option('excludeSwitches', ['enable-logging'])
servico=Service(ChromeDriverManager().install())
driver=webdriver.Chrome(service=servico, options=opts)

driver.get('https://web.whatsapp.com/')
driver.implicitly_wait(60) # seconds

col_nome = 'Nome'
col_mensagem = 'Mensagem'

infor_excel = pd.read_excel('C:\\Users\\felip\\OneDrive\\Área de 
Trabalho\\Web\\Mensagens.xlsx')
lista_msg_excel = (infor_excel[[col_nome,col_mensagem]])


def buscar_contato(contato):
campo_pesquisa = driver.find_element(By. CSS_SELECTOR,'.selectable- 
text')
sleep(2)
campo_pesquisa.click()
sleep(2)
campo_pesquisa.send_keys(str(row['Nome']))
sleep(2)
campo_pesquisa.send_keys(Keys. ENTER)
def enviar_mensagem(mensagem):
campo_mensagem = driver.find_elements(By.CSS_SELECTOR, 
'p[class="selectable-text copyable-text"]')
sleep(2)
campo_mensagem[0].click()
sleep(2)
campo_mensagem[0].send_keys(str(row['Mensagem']))
sleep(2)
campo_mensagem[0].send_keys(Keys. ENTER)

for index, row in lista_msg_excel.iterrows():
buscar_contato(col_nome) 
enviar_mensagem(col_mensagem)
6
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Sep 18, 2022 at 21:44
  • what is the problem? Do you get error or what? We can't run it, and we can't read in your mind. You have to describe all details in question (not in comments)
    – furas
    Sep 18, 2022 at 22:57
  • always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and easier to use in answer (simpler to select and copy), and more people will see it - so more people can help you.
    – furas
    Sep 18, 2022 at 22:57
  • get() opens browser only once. I don't understand why you don't want to use it again. Besides, if after sending first message it doesn't show page for next mesasge then you may have to get() the same page again and you have to find element again.
    – furas
    Sep 18, 2022 at 23:00
  • This problem seems to have nothing to do with vscode. I deleted the tag for you. Sep 19, 2022 at 1:55

1 Answer 1

-1

I solved the problem, inserting several for, as below:

for index, row in lista_msg_excel.iterrows():
  buscar_contato(col_nome) 
  enviar_mensagem(col_mensagem)

sleep(1800)

for index, row in lista_msg_excel.iterrows():
   buscar_contato(col_nome) 
   enviar_mensagem(col_mensagem)

sleep(1800)

for index, row in lista_msg_excel.iterrows():
   buscar_contato(col_nome) 
   enviar_mensagem(col_mensagem)

sleep(1800)

for index, row in lista_msg_excel.iterrows():
   buscar_contato(col_nome) 
   enviar_mensagem(col_mensagem)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.