|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 8, |
| 6 | + "id": "97312e9a", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "# Importing the selenium libraries\n", |
| 11 | + "from selenium import webdriver\n", |
| 12 | + "from selenium.webdriver.common.keys import Keys\n", |
| 13 | + "\n", |
| 14 | + "# Importing the pyautogui libraries\n", |
| 15 | + "import pyautogui as waitTime\n", |
| 16 | + "import pyautogui as keyboardKeys\n", |
| 17 | + "\n", |
| 18 | + "# By to work with more recent computers\n", |
| 19 | + "from selenium.webdriver.common.by import By\n", |
| 20 | + "\n", |
| 21 | + "# Importing the openpyxl library to work with Excel\n", |
| 22 | + "from openpyxl import load_workbook\n", |
| 23 | + "\n", |
| 24 | + "# Getting the path of the file + name of the file on the computer\n", |
| 25 | + "contacts_file_name = \"A:\\\\Python RPA\\WhatsApp\\Contacts.xlsx\"\n", |
| 26 | + "contactsDataSheet = load_workbook(contacts_file_name)\n", |
| 27 | + "\n", |
| 28 | + "# Selecting the Data sheet\n", |
| 29 | + "selected_sheet = contactsDataSheet['Data']\n", |
| 30 | + "\n", |
| 31 | + "# Emulating the Chrome browser\n", |
| 32 | + "chromeBrowser = webdriver.Chrome()\n", |
| 33 | + "\n", |
| 34 | + "# Opening the web page we need to open\n", |
| 35 | + "chromeBrowser.get(\"https://web.whatsapp.com/\")\n", |
| 36 | + "\n", |
| 37 | + "# Wait until the ID 'side' is found\n", |
| 38 | + "# While the size of the list is less than 1, keep trying to log in\n", |
| 39 | + "while len(chromeBrowser.find_elements(By.ID, 'side')) < 1:\n", |
| 40 | + " # Wait 3 seconds to see if WhatsApp Web has logged in\n", |
| 41 | + " waitTime.sleep(3)\n", |
| 42 | + " \n", |
| 43 | + "# Wait 3 seconds to give the site time to process the information \n", |
| 44 | + "waitTime.sleep(3)\n", |
| 45 | + "\n", |
| 46 | + "# For loop to iterate through each contact\n", |
| 47 | + "for row in range(2, len(selected_sheet['A']) + 1):\n", |
| 48 | + " \n", |
| 49 | + " # Creating the variables name and message\n", |
| 50 | + " contactName = selected_sheet['A%s' % row].value\n", |
| 51 | + " contactMessage = selected_sheet['B%s' % row].value\n", |
| 52 | + " \n", |
| 53 | + " # Search by XPATH and type the contactName in the WhatsApp Web search field\n", |
| 54 | + " chromeBrowser.find_element(By.XPATH, '//*[@id=\"side\"]/div[1]/div/label/div/div[2]').send_keys(contactName)\n", |
| 55 | + " \n", |
| 56 | + " # Wait 3 seconds to give the site time to process the information \n", |
| 57 | + " waitTime.sleep(3)\n", |
| 58 | + " \n", |
| 59 | + " # Press the Enter key\n", |
| 60 | + " keyboardKeys.press('enter')\n", |
| 61 | + " \n", |
| 62 | + " # Wait 3 seconds to give the site time to process the information \n", |
| 63 | + " waitTime.sleep(3)\n", |
| 64 | + " \n", |
| 65 | + " # Search by XPATH for the message input field and send the message\n", |
| 66 | + " chromeBrowser.find_element(By.XPATH, '//*[@id=\"main\"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]').send_keys(contactMessage)\n", |
| 67 | + " \n", |
| 68 | + " # Wait 3 seconds to give the site time to process the information \n", |
| 69 | + " waitTime.sleep(3)\n", |
| 70 | + " \n", |
| 71 | + " # Press the Enter key to send the message\n", |
| 72 | + " keyboardKeys.press('enter')\n", |
| 73 | + " \n", |
| 74 | + " # Wait 3 seconds to give the site time to process the information \n", |
| 75 | + " waitTime.sleep(3)\n" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "id": "55a5e964", |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 5, |
| 89 | + "id": "a0093c3e", |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [], |
| 92 | + "source": [ |
| 93 | + "# Importing the selenium libraries\n", |
| 94 | + "from selenium import webdriver\n", |
| 95 | + "from selenium.webdriver.common.keys import Keys\n", |
| 96 | + "\n", |
| 97 | + "# Importing the pyautogui libraries\n", |
| 98 | + "import pyautogui as waitTime\n", |
| 99 | + "import pyautogui as keyboardKeys\n", |
| 100 | + "\n", |
| 101 | + "# By to work with more recent computers\n", |
| 102 | + "from selenium.webdriver.common.by import By\n", |
| 103 | + "\n", |
| 104 | + "# Importing the openpyxl library to work with Excel\n", |
| 105 | + "from openpyxl import load_workbook\n", |
| 106 | + "\n", |
| 107 | + "# Getting the path of the file + name of the file on the computer\n", |
| 108 | + "contacts_file_name = \"A:\\\\Python RPA\\WhatsApp\\Contacts.xlsx\"\n", |
| 109 | + "contactsDataSheet = load_workbook(contacts_file_name)\n", |
| 110 | + "\n", |
| 111 | + "# Selecting the Data sheet\n", |
| 112 | + "selected_sheet = contactsDataSheet['Data']\n", |
| 113 | + "\n", |
| 114 | + "# Emulating the Chrome browser\n", |
| 115 | + "chromeBrowser = webdriver.Chrome()\n", |
| 116 | + "\n", |
| 117 | + "# Opening the web page we need to open\n", |
| 118 | + "chromeBrowser.get(\"https://web.whatsapp.com/\")\n", |
| 119 | + "\n", |
| 120 | + "# Wait until the ID 'side' is found\n", |
| 121 | + "# While the size of the list is less than 1, keep trying to log in\n", |
| 122 | + "while len(chromeBrowser.find_elements(By.ID, 'side')) < 1:\n", |
| 123 | + " # Wait 3 seconds to see if WhatsApp Web has logged in\n", |
| 124 | + " waitTime.sleep(3)\n", |
| 125 | + " \n", |
| 126 | + "# Wait 3 seconds to give the site time to process the information \n", |
| 127 | + "waitTime.sleep(3)\n", |
| 128 | + "\n", |
| 129 | + "image = \"A:\\\\Python RPA\\WhatsApp\\Good_Night.jpg\"\n", |
| 130 | + "\n", |
| 131 | + "# For loop to iterate through each contact\n", |
| 132 | + "for row in range(2, len(selected_sheet['A']) + 1):\n", |
| 133 | + " \n", |
| 134 | + " # Creating the variables name and message\n", |
| 135 | + " contactName = selected_sheet['A%s' % row].value\n", |
| 136 | + " contactMessage = selected_sheet['B%s' % row].value\n", |
| 137 | + " \n", |
| 138 | + " # Search by XPATH and type the contactName in the WhatsApp Web search field\n", |
| 139 | + " chromeBrowser.find_element(By.XPATH, '//*[@id=\"side\"]/div[1]/div/label/div/div[2]').send_keys(contactName)\n", |
| 140 | + " \n", |
| 141 | + " # Wait 3 seconds to give the site time to process the information \n", |
| 142 | + " waitTime.sleep(3)\n", |
| 143 | + " \n", |
| 144 | + " # Press the Enter key\n", |
| 145 | + " keyboardKeys.press('enter')\n", |
| 146 | + " \n", |
| 147 | + " # Wait 3 seconds to give the site time to process the information \n", |
| 148 | + " waitTime.sleep(3)\n", |
| 149 | + " \n", |
| 150 | + " # CSS Selectors are used to identify elements that do not have ID or Name\n", |
| 151 | + " # We are getting all the span tags with the data-icon attribute containing 'clip'\n", |
| 152 | + " chromeBrowser.find_elements(By.CSS_SELECTOR, \"span[data-icon='clip']\")\n", |
| 153 | + " \n", |
| 154 | + " # Wait 2 seconds to give the site time to process the information \n", |
| 155 | + " waitTime.sleep(2)\n", |
| 156 | + " \n", |
| 157 | + " # Click the clip icon\n", |
| 158 | + " chromeBrowser.find_element(By.CSS_SELECTOR, \"span[data-icon='clip']\").click()\n", |
| 159 | + " \n", |
| 160 | + " # Click the image icon\n", |
| 161 | + " # We are getting all the span tags with the input attribute containing 'file'\n", |
| 162 | + " fileOptions = chromeBrowser.find_element(By.CSS_SELECTOR, \"input[type='file']\")\n", |
| 163 | + " \n", |
| 164 | + " # Wait 1 second to give the site time to process the information \n", |
| 165 | + " waitTime.sleep(1)\n", |
| 166 | + " \n", |
| 167 | + " # Send the image from the computer to WhatsApp Web\n", |
| 168 | + " fileOptions.send_keys(image)\n", |
| 169 | + " \n", |
| 170 | + " # Wait 3 seconds to give the site time to process the information \n", |
| 171 | + " waitTime.sleep(3)\n", |
| 172 | + " \n", |
| 173 | + " # Search by XPATH for the message input field and send the message\n", |
| 174 | + " chromeBrowser.find_element(By.XPATH, '//*[@id=\"app\"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/div/div[1]/div[3]/div/div/div[ |
0 commit comments