|
| 1 | +#!/usr/bin/env python3 |
| 2 | +#Import and dependencies |
| 3 | + |
| 4 | +from selenium import webdriver |
| 5 | +import time |
| 6 | +from selenium.webdriver.common.keys import Keys |
| 7 | + |
| 8 | +#Constants |
| 9 | +three = 3 |
| 10 | +twenty-five = 25 |
| 11 | +one = 1 |
| 12 | + |
| 13 | +#Here the process of automation is achieved by using the framework Selenium. |
| 14 | +#Selenium is a portable framework for testing and automating web applications web applications |
| 15 | + |
| 16 | +#Path to chromedriver.exe |
| 17 | +chrome_path = r"C:\Users\giril\AppData\Local\Programs\Python\Python36-32\chromedriver.exe" |
| 18 | + |
| 19 | +#Initiating and setting up the driver |
| 20 | +driver = webdriver.Chrome(chrome_path) |
| 21 | + |
| 22 | +URL = "https://web.whatsapp.com/" |
| 23 | +driver.get(URL) |
| 24 | + |
| 25 | +#The script is made to wait, to ensure that the page is loaded |
| 26 | +time.sleep(twenty-five) |
| 27 | + |
| 28 | +def send_messages(): |
| 29 | + |
| 30 | + chats = int(input("Enter the number of personal chats/groups you would want to message ")) |
| 31 | + |
| 32 | + for chat in range(chats): |
| 33 | + |
| 34 | + #This is used to enable the user to search for the contact |
| 35 | + driver.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/div/header/div[2]/div/span/div[2]/div').click() |
| 36 | + |
| 37 | + name = input("Enter name to whom you want to send a message ") |
| 38 | + time.sleep(three) |
| 39 | + |
| 40 | + #On entering the contact, the name is entered into the search bar |
| 41 | + 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]').send_keys(name) |
| 42 | + time.sleep(three) |
| 43 | + |
| 44 | + #The contact's chat will be opened |
| 45 | + 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]').send_keys(Keys.ENTER) |
| 46 | + |
| 47 | + #This enables the user to send more than one message to the same contact |
| 48 | + done = one |
| 49 | + while done: |
| 50 | + message = input("Enter the message you want to enter ") |
| 51 | + |
| 52 | + #The cursor is positioned and the chat box is activated |
| 53 | + driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[2]/div/div[2]').send_keys(message) |
| 54 | + time.sleep(three) |
| 55 | + |
| 56 | + #On clicking the send button, a message is sent to the contact |
| 57 | + driver.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[3]/button').click() |
| 58 | + |
| 59 | + print("Would you want to send another message to the same contact ?") |
| 60 | + |
| 61 | + done = int(input("Enter 1 to continue, 0 to stop ")) |
| 62 | + |
| 63 | + print("All messages sent successfully") |
| 64 | + |
| 65 | +send_messages() |
0 commit comments