Selenium provides us with the power to automate web browsers. Initially, Selenium was designed to automate web testing. However, we can utilize it to automate our daily administrative tasks. In this Answer, we will try to send a Whatsapp message using Selenium by automating a web browser to interact with the WhatsApp web interface.
Following is the code example to automate sending messages on Whatsapp.
Note: Replace
phone_number
in line 13 with the real phone number in the international format before running the following example.
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.keys import Keys phone = "phone_number" message = 'Hi There. This is test message from educative.io' options = Options() options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') prefs = {"download.default_directory": "."} options.add_experimental_option("prefs", prefs) browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) BASE_URL = "https://web.whatsapp.com/" CHAT_URL = "https://web.whatsapp.com/send?phone={phone}&text&type=phone_number&app_absent=1" browser.get(BASE_URL) print("Kindly go to output tab and login before the timeout of 120 seconds.") # Wait for the learner to log in try: wait = WebDriverWait(browser, 120) # Adjust the timeout value as needed (e.g., 60 seconds) wait.until(EC.presence_of_element_located((By.XPATH, "//div[@contenteditable='true']"))) print("Logged in successfully. Proceeding to send message") except Exception as e: print("Timeout: Learner did not log in within the specified time.") browser.quit() browser.get(CHAT_URL.format(phone=phone)) time.sleep(10) try: wait = WebDriverWait(browser, 30) # Adjust the timeout value as needed (e.g., 60 seconds) wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div/p'))) inp_xpath = ('//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div/p') input_box = WebDriverWait(browser, 60).until(expected_conditions.presence_of_element_located((By.XPATH, inp_xpath))) input_box.send_keys(message) input_box.send_keys(Keys.ENTER) time.sleep(10) except Exception as e: print("Kindy Enter the valid number.") browser.quit()
Lines 1–11: Importing the required libraries.
Lines 13–14: Defining the variables to save the phone number and message.
Lines 16–21: Defining the setting options of the Chrome browser and browser
variable.
Lines 23–24: Defining the variable for the URL of Whatsapp and the chat room URL.
Lines 26–35: Opening the base URL of Whatsapp and waiting for the learner to log in.
Lines 38–50: Opening the chat room and sending the specified message after verifying that the number is valid.
Running the code above will send a message to the phone number we provide in the code.
Free Resources