Home > other >  How to make sure the last message sent in WhatsApp python bot
How to make sure the last message sent in WhatsApp python bot

Time:04-22

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?

CodePudding user response:

when selenium clicks "send msg button",
a new div will be created in html that indicates that you sent a msg. so, make selenium wait until it appears.

then make selenium also wait for the verification icon that indicates that the msg has been sent successfully to the Whatsapp server.


Q: How do I get the new message ? A: you can wait until the chat-room html-div contains the msg you sent.

from selenium.webdriver.support import expected_conditions as EC

locator = (By.ID, 'someid') #modify this according to your case
your_new_msg = 'blabla bla' #modify this according to your case
wait.until(EC.text_to_be_present_in_element(locator, your_new_msg))

but this short solution may lead to bug because the msg may be existing within another old msg in the chat-room div. so, selenium will think that it is the intended msg then exit the wait by mistake.
so, the solution for this may be hard for you if you are a new developer.
you need to make a channel between Python and Javascript to pass the data between them
then you create an Event Listener in JS to watch the chat-room changes (eg: you can override the (chat-room-div).appendChild method. then you get the new-msg html element in your JS method before it is shown on html)
you then can send a msg from JS to python to tell him that the new-msg div has appeared and the verification icon also appeared
then python will exit its waiting state. then continue executing the next code _____ this is just the idea. the implementation of it is up to you.
  • Related