I use the code I wrote to send a message on WhatsApp to several contacts, but before all the messages are sent, Chrome is closed and some messages are not sent. How can I be sure that the message I sent was sent? (This is not a problem in the program) The problem is the low speed of the Internet and you have to wait a while for the message to be sent
from bs4 import BeautifulSoup
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import os
class WhatsApp:
def __init__(self) -> None:
user = os.getlogin()
options = webdriver.ChromeOptions()
# options.headless = True
options.add_argument('--profile-directory=Default')
options.add_argument(
f'--user-data-dir=C:\\Users\\{user}\\AppData\\Local\\Google\\Chrome\\User Data')
PATH = "chromedriver_win32\chromedriver.exe"
self.driver = webdriver.Chrome(
executable_path=PATH, chrome_options=options)
self.driver.set_window_position(0, 0)
self.driver.set_window_size(0, 0)
self.driver.get("https://web.whatsapp.com")
WebDriverWait(self.driver, 5000).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="side"]/header/div[2]/div/span/div[2]/div'))
)
def send_msg(self, phone: str, msg: str):
search_btn = self.driver.find_element_by_xpath(
'//*[@id="side"]/header/div[2]/div/span/div[2]/div')
search_btn.send_keys(Keys.ENTER)
input_search = self.driver.find_element_by_xpath(
'/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/div/div[2]')
input_search.send_keys(phone, Keys.ENTER)
try:
sleep(1)
self.driver.find_element_by_xpath(
"/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]").send_keys(
msg, Keys.ENTER)
except Exception as e:
pass
# showMessageBox.contacte_not_found(showMessageBox)
def check_is_sended(self, phone: str, msg: str):
pass
#some code i need to check message sended or not
def END(self):
sleep(3)
self.driver.close()
app = WhatsApp()
phone = "+98xxxxxxxx87"
message = "test"
app.send_msg(phone , message)
app.END()
so I do not want to use sleep for long time im just want to find best way to make time short for runing program any id?

that indicates that the msg has been sent successfully to the Whatsapp server.