# get the current date in the required format import datetime # store the dates of birth of your contacts import json from selenium import webdriver # add a delay so that all elements # web page loads before proceeding import time # Global variable Do not use elsewhere eleNM = None # This function is just to return # message string required def wish_birth (name ): return "Happy Birthday " + name.split ( " " ) [ 0 ] + "!!" # This function returns a list of values for some # attribute based subject to two attributes from the JSON file. # use to return the names of contacts who have a birthday on the current date. def getJsonData ( file , attr_ret, attr1, attr2, attr_val1, attr_val2): # Load file data into data variable data = json.load ( file ) retv = [] # If the attribute value conditions are met # add name to list to return. for i in data: if (i [attr1] = = attr_val1 and i [attr2] = = attr_val2): retv.append (i [attr_ret]) return retv # Opens the JSON file (birthdays.json) in read-only mode. data_file = open ( "birthdays.json " , " r " ) namev = [] print ( "Script Running" ) # This will repeat the part # code from & # 39; while True & # 39; before & # 39; break & # 39 ;. # use to wait for the JSON function # to return a non-empty list. # In practice this function will be repeated when # 11:59 pm on day before birthday and flare up at 12:00. while True : try : # to get the current date datt = datetime. datetime.now () namev = getJsonData (data_file, " name " , " birth_month " , "birth_date" , str (datt.month), str (datt.day)) except json.decoder.JSONDecodeError: continue if (namev! = []): break # ChromeOptions n Allows us to use custom chrome data # so you don’t have to log in manually each time. chropt = webdriver.ChromeOptions () # add the userdata argument to the ChromeOptions chropt.add_argument ( "user-data- "LOCATION TO YOUR CHROME USER DATA"" ) # Create a Chrome Webdriver object driver = webdriver.Chrome (executable_path = ""LOCATION TO CHROME WEBDRIVER"" , options = chropt) driver.get ( " https://web.whatsapp.com/ " ) # a delay added to allow time for all items to load time.sleep ( 10 ) print (namev) # Find your contacts’ chat (as in the namev list) for inp in namev: try : eleNM = driver.find_element_by_xpath ( ’ // span [@title = "{}"] ’ . format (inp)) except Exception as ex: print (ex) continue # Simulates a mouse click on an element eleNM.click () < code class = "keyword"> while ( True ): # Finds a chat element eleTF = driver.find_element_by_class_name ( "_ 13mgZ" ) # Writes a message (call to the wish_birth () function) eleTF. send_keys (wish_birth (inp)) # Finds the Submit button eleSND = driver.find_element_by_class_name ( "_ 3M-N-" ) # Simulates clicking on it eleSND.click () break |