How to send the same message multiple times?

Question:

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)
Asked By: Felipe

||

Answers:

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)
Answered By: Felipe
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.