Skip to content

Instantly share code, notes, and snippets.

@formysister
Created September 10, 2023 15:42
Show Gist options
  • Save formysister/1eb2e7e668aef0b9445f7f40968be505 to your computer and use it in GitHub Desktop.
Save formysister/1eb2e7e668aef0b9445f7f40968be505 to your computer and use it in GitHub Desktop.
selenium-whatsapp-messanger
import numpy
from selenium import webdriver
from time import sleep
import util
#load the contacts to be sent from the csv
sendToContacts = util.filter_the_contacts("contact.csv");
#Add your path to the chromedriver where you have installed it
driver = webdriver.Chrome('C:/Users/varly/OneDrive/Desktop/PYTHON/chromedriver_win32/chromedriver.exe')
driver.get('https://web.whatsapp.com')
#This will wait for 15 seconds until you scan the qr code and start the whatsapp
sleep(15)
contacts = dict()
for key, value in sendToContacts.items():
greeting_msg = "Hello, *"
content_msg = "* Merry Christmas!! May this Christmas end the present year on a cheerful note and make way for a fresh and bright New Year. May the joy and peace of Christmas be with you all through the Year. Here’s wishing you a Merry Christmas and a Happy New Year!!"
input_box = driver.find_element_by_css_selector("input[type='text']")
input_box.click()
input_box.send_keys(key)
sleep(2)
userbox = driver.find_element_by_css_selector("span[title='"+key+"']")
userbox.click()
inputbox = driver.find_element_by_css_selector("div[data-tab='1']")
inputbox.click()
inputbox.send_keys(greeting_msg + value + content_msg)
send_button = driver.find_element_by_css_selector("span[data-icon='send']")
send_button.click()
sleep(2)
import pandas as panda
def filter_the_contacts(filename):
all_contacts = panda.read_csv(filename)
all_contacts = all_contacts[["Name", "Given Name"]]
print("Instructions:" + "\nTo break, Enter c")
print("To keep it same as the given name present after - , press k")
wishing_contacts = dict()
for index, row in all_contacts.iterrows():
print(str(row['Name'])+" - "+str(row['Given Name']))
y = input()
if y == 'c':
break
if y == 'k':
wishing_contacts[str(row['Name'])] = str(row['Given Name'])
elif y != '':
wishing_contacts[str(row['Name'])] = y
return wishing_contacts
@formysister
Copy link
Author

formysister commented Sep 10, 2023

In this code, comments are added to provide explanations and additional information about the different parts of the code. Here's a breakdown of the code comments:

Set the path to the WebDriver executable: Specifies that the webdriver_path variable should be set to the actual path of the chromedriver executable on your system.

Create a new instance of the Chrome driver: Indicates that a new instance of the Chrome WebDriver is being created.

Open WhatsApp Web: Indicates that the WhatsApp Web page is being opened in the WebDriver.

Wait for the user to scan the QR code: Instructs the user to scan the QR code with their mobile phone and press Enter to continue.

Find the search bar and enter the contact or group name: Explains that the search bar element is being located and the desired contact or group name is being entered.

Wait for the chat to load: Specifies that a short delay is being added to allow time for the chat to load.

Find the input field and send the message: Indicates that the input field is being located and the desired message is being entered and sent.

Close the browser: Informs that the WebDriver and the browser window will be closed.

Adding comments to your code helps improve readability, understandability, and maintainability. It allows others (and yourself) to easily understand the purpose and functionality of each code section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment