Search code examples
pythonpython-3.xseleniumwhatsapp

Send message to multiple contacts on WhatsApp without saving the contact


I try to send a message to several contacts in WhatsApp using the following code but for each contact the page loads from the beginning and this is very time consuming.

code :

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
from selenium.webdriver.common.action_chains import ActionChains

import os


class WhatsApp:
    def __init__(self) -> None:
        user = os.getlogin()

        options = webdriver.ChromeOptions()

        options.add_argument('--profile-directory=Default')
        options.add_argument(
            f'--user-data-dir=C:\\Users\\{user}\\AppData\\Local\\Google\\Chrome\\User Data')

        PATH = os.path.dirname(__file__) + "\\chromedriver.exe"
        self.driver = webdriver.Chrome(
            executable_path=PATH, chrome_options=options)

        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):
        url = "https://web.whatsapp.com/send?phone=" + phone + "&text=" + msg + "&app_absent=1"
        self.driver.get(url)
        enter_action = ActionChains(self.driver)
        enter_action.send_keys(Keys.ENTER)
        # Send message
        enter_action.perform()


    def END(self):
        # check the message sended
        WebDriverWait(self.driver, 9000).until(
            EC.invisibility_of_element_located((By.XPATH, '//span[@data-icon="status-time"]')))
        sleep(3)
        self.driver.close()

Problem in send_msg


Solution

  •                 try
                {
                    numbers[sendCounter] = numbers[sendCounter].Trim();
                    if (numbers[sendCounter] != "")
                    {
    
                        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                        string path = "https://web.whatsapp.com/send?phone=" + numbers[sendCounter].Trim() + "&text=" + Uri.EscapeDataString(textBox4.Text) + "&app_absent=0";
                        driver.Navigate().GoToUrl(path);
                        Thread.Sleep(3000);
                        driver.FindElement(By.CssSelector("span[data-icon='clip']")).Click();
    
                        if (File.Exists(button3.Text))
                        {
                            driver.FindElement(By.CssSelector("input[type='file']")).SendKeys(button3.Text);
                        }
                        driver.FindElement(By.XPath("/html/body/div[1]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/div/div[2]/div[2]/div/div/span")).Click();
    
                        delivered++;
                        Thread.Sleep(1000);
                    }
                }
                catch
                {
                    failed++;
                }
                sendCounter++;