From b8d17503a7d093f2e22bff6e644c5492dc58e786 Mon Sep 17 00:00:00 2001 From: phanicode <63126072+phanicode@users.noreply.github.com> Date: Thu, 19 Nov 2020 23:22:34 +0530 Subject: [PATCH 1/3] Update PyWhatsapp.py --- PyWhatsapp.py | 82 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/PyWhatsapp.py b/PyWhatsapp.py index 8e80706..2681a91 100644 --- a/PyWhatsapp.py +++ b/PyWhatsapp.py @@ -1,4 +1,6 @@ import schedule +# Importing traceback to catch xml button not found errors in the future +import traceback from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC @@ -174,13 +176,19 @@ def send_unsaved_contact_message(): def send_attachment(): # Attachment Drop Down Menu - clipButton = browser.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/div/span') - clipButton.click() + try: + clipButton = browser.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/div/span') + clipButton.click() + except: + traceback.print_exc() time.sleep(1) # To send Videos and Images. - mediaButton = browser.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/span/div/div/ul/li[1]/button') - mediaButton.click() + try: + mediaButton = browser.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/span/div/div/ul/li[1]/button') + mediaButton.click() + except: + traceback.print_exc() time.sleep(3) hour = datetime.datetime.now().hour # After 5am and before 11am scheduled this. @@ -198,34 +206,64 @@ def send_attachment(): autoit.control_click("Open", "Button1") time.sleep(3) - whatsapp_send_button = browser.find_element_by_xpath( - '//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span[2]/div/div/span') - whatsapp_send_button.click() - + # Send button + try: + whatsapp_send_button = browser.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div') + whatsapp_send_button.click() + except: + traceback.print_exc() + + print("File sent") -# Function to send Documents(PDF, Word file, PPT, etc.) def send_files(): global doc_filename # Attachment Drop Down Menu - clipButton = browser.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/div/span') - clipButton.click() + clipButton = browser.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/div/span') + + time.sleep(1) - # To send a Document(PDF, Word file, PPT) - docButton = browser.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/span/div/div/ul/li[3]/button') - docButton.click() + # modified code to work with my particular implementation + # I use this to integrate non document file imports. + # This makes sure that gifs, images can be imported through documents folder and they display + # properly in whatsapp web. + if doc_filename.split('.')[1]=='pdf'or doc_filename.split('.')[1]=='docx'or doc_filename.split('.')[1]=='pptx' : + try: + docButton = browser.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/span/div/div/ul/li[3]/button') + + docButton.click() + except: + # Check for traceback errors with XML imports + traceback.print_exc() + else: + try: + # IMG attatchment button + docButton = browser.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/span/div/div/ul/li[1]/button') + docButton.click() + except: + # Check for traceback errors with XML imports + traceback.print_exc() + + + time.sleep(1) - docPath = os.getcwd() + "\\Documents\\" + doc_filename - - autoit.control_focus("Open", "Edit1") + + try: + autoit.control_focus("Open", "Edit1") + except : + traceback.print_exc() + + autoit.control_set_text("Open", "Edit1", (docPath)) autoit.control_click("Open", "Button1") time.sleep(3) - whatsapp_send_button = browser.find_element_by_xpath( - '//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span[2]/div/div/span') + # Changed whatsapp send button xml link. + whatsapp_send_button = browser.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div') + whatsapp_send_button.click() + print('File sent') def import_contacts(): @@ -275,12 +313,14 @@ def sender(): try: send_attachment() except: - print('Attachment not sent.') + # No need to print File not sent as try except statements are in function. + print() if (docChoice == "yes"): try: send_files() except: - print('Files not sent') + # No need to print File not sent as try except statements are in function. + print() time.sleep(7) From 77464c9b2d2c87a473276ae7eb058089265753d9 Mon Sep 17 00:00:00 2001 From: phanicode <63126072+phanicode@users.noreply.github.com> Date: Thu, 19 Nov 2020 23:25:57 +0530 Subject: [PATCH 2/3] Update PyWhatsapp.py --- PyWhatsapp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PyWhatsapp.py b/PyWhatsapp.py index 2681a91..4558c57 100644 --- a/PyWhatsapp.py +++ b/PyWhatsapp.py @@ -220,7 +220,7 @@ def send_files(): global doc_filename # Attachment Drop Down Menu clipButton = browser.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/div/span') - + clipButton.click() time.sleep(1) # To send a Document(PDF, Word file, PPT) From 03ca6f8d4fb2f7ac1d0c06e86984c720175bd6c1 Mon Sep 17 00:00:00 2001 From: phanicode <63126072+phanicode@users.noreply.github.com> Date: Thu, 19 Nov 2020 23:56:32 +0530 Subject: [PATCH 3/3] Update PyWhatsapp.py --- PyWhatsapp.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/PyWhatsapp.py b/PyWhatsapp.py index 4558c57..dfeb2b4 100644 --- a/PyWhatsapp.py +++ b/PyWhatsapp.py @@ -224,8 +224,6 @@ def send_files(): time.sleep(1) # To send a Document(PDF, Word file, PPT) - # modified code to work with my particular implementation - # I use this to integrate non document file imports. # This makes sure that gifs, images can be imported through documents folder and they display # properly in whatsapp web. if doc_filename.split('.')[1]=='pdf'or doc_filename.split('.')[1]=='docx'or doc_filename.split('.')[1]=='pptx' : @@ -244,18 +242,12 @@ def send_files(): except: # Check for traceback errors with XML imports traceback.print_exc() - - - time.sleep(1) docPath = os.getcwd() + "\\Documents\\" + doc_filename - try: autoit.control_focus("Open", "Edit1") except : traceback.print_exc() - - autoit.control_set_text("Open", "Edit1", (docPath)) autoit.control_click("Open", "Button1") time.sleep(3) @@ -265,7 +257,6 @@ def send_files(): whatsapp_send_button.click() print('File sent') - def import_contacts(): global Contact, unsaved_Contacts Contact = [] @@ -313,13 +304,11 @@ def sender(): try: send_attachment() except: - # No need to print File not sent as try except statements are in function. print() if (docChoice == "yes"): try: send_files() except: - # No need to print File not sent as try except statements are in function. print() time.sleep(7) @@ -340,8 +329,7 @@ def scheduler(): while True: schedule.run_pending() time.sleep(1) - - + if __name__ == "__main__": print("Web Page Open")