Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 1c6d620

Browse files
authored
Merge pull request #235 from hardik-dadhich/gmail-automation
Gmail automation of login and send mail using selenium
2 parents 0564112 + b1e3841 commit 1c6d620

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Script Title
2+
3+
<!--Remove the below lines and add yours -->
4+
5+
A Python script that automate opening of gmail using your Credentials and sends mails to sender.
6+
7+
### Prerequisites
8+
9+
<!--Remove the below lines and add yours -->
10+
11+
Modules required to be able to use the script successfully and how to install them.
12+
`pip install -r requirements.txt`
13+
14+
### How to run the script
15+
16+
<!--Remove the below lines and add yours -->
17+
18+
```bash
19+
cd Scripts/Miscellaneous/Gmail_Automation_using_selenium/
20+
```
21+
22+
1. create details.ini file by `vi details.ini`
23+
24+
2. paste these following details in `details.ini` and replace values.
25+
26+
```
27+
[login_details]
28+
username_for_email=<your email_add>
29+
password_for_email=<your password>
30+
sending_email_add=<senders email addres>
31+
driverpath=<chrome driver path of your machine>
32+
url_path=https://mail.google.com/mail/u/0/#inbox
33+
```
34+
35+
3. Then run:
36+
`python gmail_automation.py`
37+
38+
### Screenshot/GIF showing the sample use of the script
39+
40+
![Screenshot](gmail_mail_forward.png)
41+
42+
## _Author Name_
43+
44+
[Hardik_Dadhich](https://github.com/hardik-dadhich)
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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()
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
selenium==3.141.0
2+
beautifulsoup4==4.9.1

0 commit comments

Comments
 (0)