forked from Bruuuuuuce/PKUAutoSubmit
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
194 lines (171 loc) · 6.84 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from selenium.webdriver.chrome import options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Firefox, Chrome, PhantomJS
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from argparse import ArgumentParser
from urllib.parse import quote
import time
import copy
import sys
import os
TIMEOUT = 20
TIMESLP = 3
CONFIGCODE = 1
def login(driver, username, password, failed=0):
if failed == 3:
raise Exception('门户登录失败')
iaaaUrl = 'https://iaaa.pku.edu.cn/iaaa/oauth.jsp'
appName = quote('北京大学校内信息门户新版')
redirectUrl = 'https://portal.pku.edu.cn/portal2017/ssoLogin.do'
driver.get('https://portal.pku.edu.cn/portal2017/')
driver.get(
f'{iaaaUrl}?appID=portal2017&appName={appName}&redirectUrl={redirectUrl}'
)
print('门户登陆中...')
driver.find_element_by_id('user_name').send_keys(username)
time.sleep(TIMESLP)
driver.find_element_by_id('password').send_keys(password)
time.sleep(TIMESLP)
driver.find_element_by_id('logon_button').click()
try:
WebDriverWait(driver, TIMEOUT).until(
EC.visibility_of_element_located((By.LINK_TEXT, '我知道了')))
except:
pass
else:
driver.find_element_by_link_text('我知道了').click()
try:
WebDriverWait(driver, TIMEOUT).until(
EC.visibility_of_element_located((By.ID, 'all')))
except:
login(driver, username, password, failed + 1)
else:
print('门户登录成功!')
def go_to_application(driver):
driver.find_element_by_id('all').click()
if CONFIGCODE == 0:
WebDriverWait(driver, TIMEOUT).until(
EC.visibility_of_element_located((By.ID, 'tag_s_epidemic')))
driver.find_element_by_id('tag_s_epidemic').click()
time.sleep(TIMESLP)
driver.switch_to.window(driver.window_handles[-1])
WebDriverWait(driver, TIMEOUT).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-input__inner')))
print('已进入燕园云战疫!')
else:
WebDriverWait(driver, TIMEOUT).until(
EC.visibility_of_element_located((By.ID, 'tag_s_stuCampusExEnReq')))
driver.find_element_by_id('tag_s_stuCampusExEnReq').click()
time.sleep(TIMESLP)
driver.switch_to.window(driver.window_handles[-1])
WebDriverWait(driver, TIMEOUT).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-card__body')))
print('已进入学生出入校!')
def click_no(driver):
print("选择是否存在以下症状:否")
temp = driver.find_element_by_xpath('//*[@id="pane-daily_info_tab"]/form/div[13]/div/label[2]').click()
print("Done")
time.sleep(TIMESLP)
def select_healthy(driver):
print("选择疫情诊断:健康")
driver.find_element_by_xpath('//*[@id="pane-daily_info_tab"]/form/div[14]/div/div/div').click()
time.sleep(TIMESLP)
driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[1]/ul/li[1]").click()
print("Done")
time.sleep(TIMESLP)
def submit(driver):
print("保存信息")
driver.find_element_by_xpath('//*[@id="pane-daily_info_tab"]/form/div[17]/div/button').click()
print(driver.find_element_by_xpath('//*[@id="pane-daily_info_tab"]/form/div[17]/div/button').text)
print("Done")
time.sleep(TIMESLP)
def click_in_out(driver):
try:
driver.find_element_by_xpath('//*[@id="bizTip"]/div/div/div[1]/div/div/table/tbody/tr[11]/td/a').click()
print("点击'我知道了'")
time.sleep(TIMESLP)
except:
pass
driver.find_element_by_link_text('出入校备案').click()
print("进入出入校备案")
time.sleep(TIMESLP)
temp = driver.find_elements_by_class_name("el-button")
temp[1].click()
print("加载出校申请信息")
time.sleep(TIMESLP)
driver.find_element_by_class_name("el-checkbox__inner").click()
print("点击遵守")
time.sleep(TIMESLP)
temp[3].click()
print("保存")
time.sleep(TIMESLP)
ActionChains(driver).send_keys(Keys.ENTER).perform()
print("提交")
time.sleep(TIMESLP)
driver.find_element_by_class_name("el-page-header__left").click()
print("返回上一页")
time.sleep(TIMESLP)
driver.find_element_by_link_text('出入校备案').click()
print("进入出入校备案")
time.sleep(TIMESLP)
temp = driver.find_elements_by_class_name("el-button")
temp[2].click()
print("加载入校申请信息")
time.sleep(TIMESLP)
driver.find_element_by_class_name("el-checkbox__inner").click()
print("点击遵守")
time.sleep(TIMESLP)
temp[3].click()
print("保存")
time.sleep(TIMESLP)
ActionChains(driver).send_keys(Keys.ENTER).perform()
print("提交")
def fill(driver):
if CONFIGCODE == 0:
click_no(driver)
select_healthy(driver)
submit(driver)
else:
click_in_out(driver)
print('填报完毕!')
def run(driver, username, password):
login(driver, username, password)
print('=================================')
go_to_application(driver)
fill(driver)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--username', '-u', type=str, help='用户名')
parser.add_argument('--password', '-p', type=str, help='密码')
args = parser.parse_args()
args_public = copy.deepcopy(args)
args_public.password = 'xxxxxxxx'
print('Arguments: {}'.format(args_public))
print('Driver Launching...')
# driver = Firefox()
# driver = Chrome()
dir_path = os.path.dirname(os.path.abspath(__file__))
if sys.platform == 'darwin': # macOS
# phantomjs_path = os.path.join('phantomjs', 'phantomjs-darwin')
chrome_path = os.path.join( dir_path, 'chromedriver_mac64', 'chromedriver')
elif sys.platform == 'linux': # linux
# phantomjs_path = os.path.join('phantomjs', 'phantomjs-linux-x86_64')
chrome_path = os.path.join( dir_path, 'chromedriver_linux64', 'chromedriver')
else: # windows
# phantomjs_path = os.path.join('phantomjs', 'phantomjs-windows.exe')
chrome_path = os.path.join( dir_path, 'chromedriver_win32', 'chromedriver.exe')
# driver = PhantomJS(executable_path=phantomjs_path)
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless')
print(chrome_path)
driver = webdriver.Chrome(executable_path = chrome_path, options = chrome_options)
run(driver, args.username, args.password)
driver.close()