Skip to content

Commit 9efdd27

Browse files
committed
add: auto login & schedule to run at later date
1 parent 263e3e8 commit 9efdd27

File tree

1 file changed

+70
-34
lines changed

1 file changed

+70
-34
lines changed

main.py

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
# Copyright 2020 Eric Qian.
22
# All rights reserved.
33
import time
4+
import datetime
5+
import pause
46
import signal
57
import sys
68
from selenium import webdriver
79
from selenium.webdriver.chrome.options import Options
810
from selenium.common.exceptions import WebDriverException
911

12+
print("To automate login, Cal Poly credentials are required.")
13+
print("CalPoly Username: ")
14+
username = str(input())
15+
print("CalPoly Password: ")
16+
password = str(input())
17+
18+
print("Schedule to run at a later time? (y/n)")
19+
targetSchedule = str(input())
20+
if targetSchedule == "y" or targetSchedule == "Y":
21+
print("Enter year:")
22+
targetYear = int(input())
23+
print("Enter month:")
24+
targetMonth = int(input())
25+
print("Enter day:")
26+
targetDay = int(input())
27+
print("Enter hour(1-24):")
28+
targetHour = int(input())
29+
print("Enter minute:")
30+
targetMinute = int(input())
31+
32+
targetTime = datetime.datetime(targetYear, targetMonth, targetDay, targetHour, targetMinute, 0, 0)
33+
print ("time set: " + targetTime.strftime("%m/%d/%Y, %H:%M:%S"))
34+
pause.until(targetTime)
35+
else:
36+
print("Running NOW.")
37+
1038
opts = Options()
1139
# opts.add_argument("user-data-dir=")
1240

@@ -18,7 +46,6 @@
1846
# opts.binary_location = brave_path
1947
browser = webdriver.Chrome(executable_path=driver_path, options=opts)
2048

21-
2249
def signal_handler(sig, frame):
2350
print('Exiting...')
2451
browser.quit()
@@ -61,9 +88,9 @@ def add_class(sectionNum, laboratoryNum=-1):
6188

6289
while len(browser.find_elements_by_css_selector('[id^=trSSR_CLS_TBL_R1]')) == 0:
6390
time.sleep(1)
64-
# ERR: class already in cart
6591
errStatus, errMsg = errors_exists()
6692
if errStatus is True:
93+
# ERR: class already in cart
6794
print ("ERROR IN ADDING CLASS: " + sectionNum + ". " + errMsg)
6895
return
6996
labsections = browser.find_elements_by_css_selector('[id^=trSSR_CLS_TBL_R1]')
@@ -92,37 +119,46 @@ def add_class(sectionNum, laboratoryNum=-1):
92119
print ("added class.")
93120

94121

122+
def runProg():
123+
browser.get('https://myportal.calpoly.edu')
124+
125+
print(browser.current_url)
126+
if browser.current_url.startswith('https://idp.calpoly.edu/idp/profile/cas/login'):
127+
print("login required.")
128+
while len(browser.find_elements_by_id('username')) == 0:
129+
time.sleep(1)
130+
browser.find_element_by_id('username').send_keys(username)
131+
browser.find_element_by_id('password').send_keys(password)
132+
browser.find_element_by_name('_eventId_proceed').click()
133+
while browser.current_url.startswith('https://idp.calpoly.edu/idp/profile/cas/login'):
134+
time.sleep(1)
135+
else:
136+
print("already logged in.")
137+
138+
print("logged in.")
139+
navLinks = browser.find_elements_by_class_name("singleclick-link")
140+
for link in navLinks:
141+
if link.text == "Student Center":
142+
link.click()
143+
break
144+
if link == navLinks[len(navLinks) - 1]:
145+
print ("Student center not found in nav panel. Aborting.")
146+
sys.exit(1)
147+
browser.switch_to.window(browser.window_handles[1])
148+
if (browser.current_url.startswith("https://cmsweb.pscs.calpoly.edu/psp/CSLOPRD/EMPLOYEE/SA/c/SA_LEARNER_SERVICES.SSS_STUDENT_CENTER.GBL")):
149+
print ("now in student center.")
150+
151+
# print(browser.current_url)
152+
while len(browser.find_elements_by_id('ptifrmtgtframe')) == 0:
153+
time.sleep(1)
154+
browser.switch_to.frame(browser.find_element_by_id('ptifrmtgtframe'))
155+
enrollBtn = browser.find_element_by_css_selector("[aria-label='Enroll']")
156+
enrollBtn.click()
157+
158+
add_class('9672', '9673')
159+
add_class('3971')
160+
confirm_class_add()
95161

96-
browser.get('https://myportal.calpoly.edu')
162+
time.sleep(30)
97163

98-
print(browser.current_url)
99-
if browser.current_url.startswith('https://idp.calpoly.edu/idp/profile/cas/login'):
100-
print("login required.")
101-
while browser.current_url.startswith('https://idp.calpoly.edu/idp/profile/cas/login'):
102-
time.sleep(3)
103-
else:
104-
print("already logged in.")
105-
106-
print("logged in.")
107-
navLinks = browser.find_elements_by_class_name("singleclick-link")
108-
for link in navLinks:
109-
if link.text == "Student Center":
110-
link.click()
111-
break
112-
if link == navLinks[len(navLinks) - 1]:
113-
print ("Student center not found in nav panel. Aborting.")
114-
sys.exit(1)
115-
browser.switch_to.window(browser.window_handles[1])
116-
if (browser.current_url.startswith("https://cmsweb.pscs.calpoly.edu/psp/CSLOPRD/EMPLOYEE/SA/c/SA_LEARNER_SERVICES.SSS_STUDENT_CENTER.GBL")):
117-
print ("now in student center.")
118-
119-
# print(browser.current_url)
120-
browser.switch_to.frame(browser.find_element_by_id('ptifrmtgtframe'))
121-
enrollBtn = browser.find_element_by_css_selector("[aria-label='Enroll']")
122-
enrollBtn.click()
123-
124-
add_class('9672', '9673')
125-
add_class('3971')
126-
confirm_class_add()
127-
128-
time.sleep(30)
164+
runProg()

0 commit comments

Comments
 (0)