Skip to content

Commit 194900d

Browse files
committed
add: working selenium automation
1 parent 70c9993 commit 194900d

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
debug.log
2+
chrome-user-data/
3+
.vscode/

CalPolyAuto.zip

-82.9 KB
Binary file not shown.

main.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Copyright 2020 Eric Qian.
2+
# All rights reserved.
3+
import time
4+
import signal
5+
import sys
6+
from selenium import webdriver
7+
from selenium.webdriver.chrome.options import Options
8+
from selenium.common.exceptions import WebDriverException
9+
10+
opts = Options()
11+
# opts.add_argument("user-data-dir=")
12+
13+
driver_path = "C:/Users/Eric/OneDrive - California Polytechnic State University/Documents/chromedriver84.exe"
14+
# brave_path = "C:/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe"
15+
16+
opts.add_argument("user-data-dir=chrome-user-data")
17+
opts.add_argument("profile-directory='Profile 1'")
18+
# opts.binary_location = brave_path
19+
browser = webdriver.Chrome(executable_path=driver_path, options=opts)
20+
21+
22+
def signal_handler(sig, frame):
23+
print('Exiting...')
24+
browser.quit()
25+
sys.exit(0)
26+
27+
signal.signal(signal.SIGINT, signal_handler)
28+
print('Press Ctrl+C to stop')
29+
30+
def press_next_btn():
31+
browser.find_element_by_css_selector('[id^=DERIVED_CLS_DTL_NEXT_PB]').click()
32+
print ("next button pressed.")
33+
34+
def confirm_class_add():
35+
while len(browser.find_elements_by_css_selector("[value='Proceed to Step 2 of 3']")) == 0:
36+
time.sleep(1)
37+
browser.find_element_by_css_selector("[value='Proceed to Step 2 of 3']").click()
38+
print ("commited all classes.")
39+
40+
def add_class(sectionNum, laboratoryNum=-1):
41+
while len(browser.find_elements_by_id('DERIVED_REGFRM1_CLASS_NBR')) == 0:
42+
time.sleep(1)
43+
browser.find_element_by_id('DERIVED_REGFRM1_CLASS_NBR').send_keys(sectionNum)
44+
browser.find_element_by_css_selector("[title='Add a class using class number']").click()
45+
46+
# TODO: class already in cart handling
47+
48+
if laboratoryNum != -1:
49+
# if lab section is required/passed in.
50+
51+
while len(browser.find_elements_by_css_selector('[id^=trSSR_CLS_TBL_R1]')) == 0:
52+
time.sleep(1)
53+
labsections = browser.find_elements_by_css_selector('[id^=trSSR_CLS_TBL_R1]')
54+
55+
for labsection in labsections:
56+
57+
while len(labsection.find_elements_by_css_selector('[id^=SSR_CLS_TBL_R1_RELATE_CLASS_NBR]')) == 0:
58+
time.sleep(1)
59+
for section in labsection.find_elements_by_css_selector('[id^=SSR_CLS_TBL_R1_RELATE_CLASS_NBR]'):
60+
if section.text == laboratoryNum:
61+
print ("found lab section!")
62+
labsection.find_element_by_css_selector('[class=PSRADIOBUTTON]').click()
63+
print ("selected lab section.")
64+
# time.sleep(2)
65+
press_next_btn()
66+
break
67+
68+
while len(browser.find_elements_by_css_selector('[id^=DERIVED_CLS_DTL_WAIT_LIST_OKAY]')) == 0:
69+
time.sleep(1)
70+
browser.find_element_by_css_selector('[id^=DERIVED_CLS_DTL_WAIT_LIST_OKAY].PSCHECKBOX').click()
71+
press_next_btn()
72+
73+
print ("added class.")
74+
75+
76+
77+
browser.get('https://myportal.calpoly.edu')
78+
79+
print(browser.current_url)
80+
if browser.current_url.startswith('https://idp.calpoly.edu/idp/profile/cas/login'):
81+
print("login required.")
82+
while browser.current_url.startswith('https://idp.calpoly.edu/idp/profile/cas/login'):
83+
time.sleep(3)
84+
else:
85+
print("already logged in.")
86+
87+
print("logged in.")
88+
navLinks = browser.find_elements_by_class_name("singleclick-link")
89+
for link in navLinks:
90+
if link.text == "Student Center":
91+
link.click()
92+
break
93+
if link == navLinks[len(navLinks) - 1]:
94+
print ("Student center not found in nav panel. Aborting.")
95+
sys.exit(1)
96+
browser.switch_to.window(browser.window_handles[1])
97+
if (browser.current_url.startswith("https://cmsweb.pscs.calpoly.edu/psp/CSLOPRD/EMPLOYEE/SA/c/SA_LEARNER_SERVICES.SSS_STUDENT_CENTER.GBL")):
98+
print ("now in student center.")
99+
100+
# print(browser.current_url)
101+
browser.switch_to.frame(browser.find_element_by_id('ptifrmtgtframe'))
102+
enrollBtn = browser.find_element_by_css_selector("[aria-label='Enroll']")
103+
enrollBtn.click()
104+
105+
add_class('9672', '9673')
106+
add_class('3971')
107+
confirm_class_add()
108+
109+
time.sleep(30)

0 commit comments

Comments
 (0)