|
| 1 | +''' |
| 2 | +This script automate the gmail login process and |
| 3 | +Sending first mail from your inbox and send to the |
| 4 | +sender's Email id |
| 5 | +
|
| 6 | +Note: Please create `details.ini` in same Directory |
| 7 | +(check README.md for more Details) |
| 8 | +
|
| 9 | +''' |
| 10 | + |
| 11 | +# import essential libraries |
| 12 | +from selenium import webdriver |
| 13 | +import time |
| 14 | +import configparser |
| 15 | +from bs4 import BeautifulSoup |
| 16 | + |
| 17 | + |
| 18 | +def main(): |
| 19 | + |
| 20 | + # --------------credintials reading-----------! |
| 21 | + config = configparser.ConfigParser() |
| 22 | + config.read("details.ini") |
| 23 | + |
| 24 | + username_for_email = config.get('login_details', 'username_for_email') |
| 25 | + password_for_email = config.get('login_details', 'password_for_email') |
| 26 | + sending_email_add = config.get('login_details', 'sending_email_add') |
| 27 | + driverpath = config.get('login_details', 'driverpath') |
| 28 | + |
| 29 | + # driver path for chrome |
| 30 | + driver = webdriver.Chrome(driverpath) |
| 31 | + driver.maximize_window() |
| 32 | + driver.get(config.get('login_details', 'url_path')) |
| 33 | + |
| 34 | + # email username |
| 35 | + |
| 36 | + try: |
| 37 | + mark1 = 0 |
| 38 | + elem = driver.find_element_by_id("identifierId") |
| 39 | + elem.send_keys(username_for_email) |
| 40 | + mark1 = 1 |
| 41 | + except AttributeError as exception: |
| 42 | + |
| 43 | + if mark1 == 0: |
| 44 | + driver.refresh() |
| 45 | + time.sleep(2) |
| 46 | + elem = driver.find_element_by_id("identifierId") |
| 47 | + elem.send_keys(username_for_email) |
| 48 | + print("exception has been thown--> " + str(exception)) |
| 49 | + |
| 50 | + # paste username in username bar |
| 51 | + try: |
| 52 | + mark2 = 0 |
| 53 | + next_btn_for_email = driver.find_element_by_id('identifierNext') |
| 54 | + next_btn_for_email.click() |
| 55 | + mark2 = 1 |
| 56 | + time.sleep(5) |
| 57 | + |
| 58 | + except AttributeError as exception: |
| 59 | + if mark2 == 0: |
| 60 | + |
| 61 | + driver.refresh() |
| 62 | + time.sleep(3) |
| 63 | + driver.find_element_by_id('identifierNext').click() |
| 64 | + time.sleep(3) |
| 65 | + print("exception thrown : " + str(exception)) |
| 66 | + |
| 67 | + # paste Password in password bar |
| 68 | + try: |
| 69 | + mark3 = 0 |
| 70 | + password_field = driver.find_element_by_name('password') |
| 71 | + password_field.send_keys(password_for_email) |
| 72 | + mark3 = 1 |
| 73 | + except AttributeError as exception: |
| 74 | + if mark3 == 0: |
| 75 | + driver.find_element_by_name( |
| 76 | + 'password').send_keys(password_for_email) |
| 77 | + print("exception has been thown--> " + str(exception)) |
| 78 | + # driver.refresh() |
| 79 | + |
| 80 | + # clcik submit button |
| 81 | + try: |
| 82 | + mark4 = 0 |
| 83 | + next_btn_for_password = driver.find_element_by_id('passwordNext') |
| 84 | + next_btn_for_password.click() |
| 85 | + mark4 = 1 |
| 86 | + time.sleep(5) |
| 87 | + except AttributeError as exception: |
| 88 | + if mark4 == 0: |
| 89 | + driver.refresh() |
| 90 | + time.sleep(3) |
| 91 | + driver.find_element_by_id('passwordNext').click() |
| 92 | + |
| 93 | + print("exception has been thown---> " + str(exception)) |
| 94 | + |
| 95 | + # driver.refresh() |
| 96 | + |
| 97 | + time.sleep(8) |
| 98 | + html_code = driver.page_source |
| 99 | + soup = BeautifulSoup(html_code, "html.parser") |
| 100 | + table_obj_code = soup.findAll('table', attrs={'id': ':34'}) |
| 101 | + |
| 102 | + # selection of grid to pick first email |
| 103 | + try: |
| 104 | + mark5 = 0 |
| 105 | + for elem in table_obj_code: |
| 106 | + tr_obj = elem.findAll('tr') |
| 107 | + |
| 108 | + for tr in tr_obj: |
| 109 | + p = driver.find_element_by_id(':3e') |
| 110 | + print("object found ", tr) |
| 111 | + p.click() |
| 112 | + mark5 = 1 |
| 113 | + except AttributeError as exception: |
| 114 | + if mark5 == 0: |
| 115 | + driver.refresh() |
| 116 | + time.sleep(4) |
| 117 | + for elem in table_obj_code: |
| 118 | + tr_obj = elem.findAll('tr') |
| 119 | + |
| 120 | + for tr in tr_obj: |
| 121 | + p = driver.find_element_by_id(':3e') |
| 122 | + p.click() |
| 123 | + print("exception has been thown--> " + str(exception)) |
| 124 | + print("grid of emails not found due to slow internet !") |
| 125 | + |
| 126 | + next_soup = BeautifulSoup(driver.page_source, "html.parser") |
| 127 | + |
| 128 | + # picking first mail and opening send panel |
| 129 | + try: |
| 130 | + mark6 = 0 |
| 131 | + class_obj = next_soup.findAll('div', attrs={'class': 'amn'}) |
| 132 | + for i in class_obj: |
| 133 | + print("object found ", i) |
| 134 | + pq = driver.find_element_by_class_name('bkG') |
| 135 | + pq.click() |
| 136 | + mark6 = 1 |
| 137 | + except AttributeError as exception: |
| 138 | + if mark6 == 0: |
| 139 | + class_obj = next_soup.findAll('div', attrs={'class': 'amn'}) |
| 140 | + for i in class_obj: |
| 141 | + pq = driver.find_element_by_class_name('bkG') |
| 142 | + pq.click() |
| 143 | + |
| 144 | + print("exception has been thown--> " + str(exception)) |
| 145 | + |
| 146 | + # driver.refresh() |
| 147 | + |
| 148 | + third_soup = BeautifulSoup(driver.page_source, "html.parser") |
| 149 | + |
| 150 | + filed = third_soup.findAll('table', attrs={'class': 'GS'}) |
| 151 | + |
| 152 | + # click Send button |
| 153 | + try: |
| 154 | + flag = 0 |
| 155 | + for i in filed: |
| 156 | + |
| 157 | + # pasting sender's email id |
| 158 | + driver.find_element_by_class_name( |
| 159 | + 'vO').send_keys(sending_email_add) |
| 160 | + flag = 1 |
| 161 | + succ = driver.find_element_by_class_name('btA') |
| 162 | + |
| 163 | + # send mail |
| 164 | + succ.click() |
| 165 | + time.sleep(1) |
| 166 | + except AttributeError as exception: |
| 167 | + if flag == 0: |
| 168 | + driver.refresh() |
| 169 | + time.sleep(4) |
| 170 | + driver.find_element_by_class_name( |
| 171 | + 'vO').send_keys(sending_email_add) |
| 172 | + driver.find_element_by_class_name('btA').click() |
| 173 | + |
| 174 | + else: |
| 175 | + driver.find_element_by_class_name('btA').click() |
| 176 | + |
| 177 | + print("button not clicked succesfully! " + str(exception)) |
| 178 | + driver.refresh() |
| 179 | + |
| 180 | + print("process finished") |
| 181 | + driver.quit() |
| 182 | + |
| 183 | + |
| 184 | +# driver function |
| 185 | +if __name__ == "__main__": |
| 186 | + main() |
0 commit comments