-
Notifications
You must be signed in to change notification settings - Fork 272
/
func.py
283 lines (216 loc) · 9.08 KB
/
func.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from urllib.parse import quote
from urllib import request
import time
import warnings
import json
warnings.filterwarnings('ignore')
def login(driver, userName, password, retry=0):
if retry == 3:
raise Exception('门户登录失败')
print('门户登陆中...')
appID = 'portal2017'
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={appID}&appName={appName}&redirectUrl={redirectUrl}')
WebDriverWait(driver, 5).until(
EC.visibility_of_element_located((By.ID, 'logon_button')))
driver.find_element_by_id('user_name').send_keys(userName)
time.sleep(0.1)
driver.find_element_by_id('password').send_keys(password)
time.sleep(0.1)
driver.find_element_by_id('logon_button').click()
try:
WebDriverWait(driver,
5).until(EC.visibility_of_element_located((By.ID, 'all')))
print('门户登录成功!')
except:
print('Retrying...')
login(driver, userName, password, retry + 1)
def go_to_simso(driver):
butt_all = driver.find_element_by_id('all')
driver.execute_script('arguments[0].click();', butt_all)
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'tag_s_stuCampusExEnReq')))
driver.find_element_by_id('tag_s_stuCampusExEnReq').click()
time.sleep(2)
driver.switch_to.window(driver.window_handles[-1])
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-card__body')))
def go_to_application_out(driver):
go_to_simso(driver)
driver.find_element_by_class_name('el-card__body').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-select')))
def go_to_application_in(driver, userName, password):
driver.back()
time.sleep(0.5)
driver.back()
try:
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-card__body')))
time.sleep(0.5)
driver.find_element_by_class_name('el-card__body').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-select')))
except:
print('检测到会话失效,重新登陆中...')
login(driver, userName, password)
go_to_simso(driver)
driver.find_element_by_class_name('el-card__body').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-select')))
def select_in_out(driver, way):
driver.find_element_by_class_name('el-select').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, f'//li/span[text()="{way}"]')))
driver.find_element_by_xpath(f'//li/span[text()="{way}"]').click()
def select_campus(driver, campus):
driver.find_elements_by_class_name('el-select')[1].click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, f'//li/span[text()="{campus}"]')))
driver.find_element_by_xpath(f'//li/span[text()="{campus}"]').click()
def write_reason(driver, reason):
driver.find_elements_by_class_name('el-select')[2].click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, f'//li/span[text()="{reason}"]')))
driver.find_element_by_xpath(f'//li/span[text()="{reason}"]').click()
def select_destination(driver, destination):
driver.find_elements_by_class_name('el-select')[3].click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, f'//li/span[text()="{destination}"]')))
driver.find_element_by_xpath(f'//li/span[text()="{destination}"]').click()
def select_district(driver, district):
driver.find_elements_by_class_name('el-select')[4].click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, f'//li/span[text()="{district}"]')))
driver.find_element_by_xpath(f'//li/span[text()="{district}"]').click()
def write_reason_detail(driver, detail):
driver.find_element_by_class_name('el-textarea__inner').send_keys(
f'{detail}')
time.sleep(0.1)
def write_track(driver, track):
driver.find_elements_by_class_name('el-textarea__inner')[1].send_keys(
f'{track}')
time.sleep(0.1)
def write_street(driver, street):
driver.find_elements_by_class_name('el-textarea__inner')[1].send_keys(
f'{street}')
time.sleep(0.1)
def click_check(driver):
driver.find_element_by_class_name('el-checkbox__label').click()
time.sleep(0.1)
def click_inPeking(driver):
driver.find_element_by_class_name('el-radio__inner').click()
time.sleep(0.1)
def submit(driver):
driver.find_element_by_xpath(
'//button/span[contains(text(),"保存")]').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, '(//button/span[contains(text(),"提交")])[3]')))
driver.find_element_by_xpath(
'(//button/span[contains(text(),"提交")])[3]').click()
time.sleep(0.1)
def fill_out(driver, campus, reason, detail, destination, track):
print('开始填报出校备案')
print('选择出校/入校 ', end='')
select_in_out(driver, '出校')
print('Done')
print('选择校区 ', end='')
select_campus(driver, campus)
print('Done')
print('填写出入校事由 ', end='')
write_reason(driver, reason)
print('Done')
print('填写出入校事由详细描述 ', end='')
write_reason_detail(driver, detail)
print('Done')
print('选择出校目的地 ', end='')
select_destination(driver, destination)
print('Done')
print('填写出校行动轨迹 ', end='')
write_track(driver, track)
print('Done')
click_check(driver)
submit(driver)
print('出校备案填报完毕!')
def fill_in(driver, campus, reason, detail, habitation, district, street):
print('开始填报入校备案')
print('选择出校/入校 ', end='')
select_in_out(driver, '入校')
print('Done')
print('填写出入校事由 ', end='')
write_reason(driver, reason)
print('Done')
print('填写出入校事由详细描述 ', end='')
write_reason_detail(driver, detail)
print('Done')
if habitation != '北京':
raise Exception('暂不支持京外入校备案,请手动填写')
print('选择居住地所在区 ', end='')
select_district(driver, district)
print('Done')
print('填写居住地所在街道 ', end='')
write_street(driver, street)
print('Done')
click_inPeking(driver)
click_check(driver)
submit(driver)
print('入校备案填报完毕!')
def screen_capture(driver, path):
driver.back()
time.sleep(0.5)
driver.back()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-card__body')))
driver.find_elements_by_class_name('el-card__body')[1].click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, '//button/span[contains(text(),"加载更多")]')))
driver.maximize_window()
time.sleep(0.1)
driver.save_screenshot(path + 'result.png')
print('备案历史截图已保存')
def wechat_notification(userName, sckey):
with request.urlopen(
quote('https://sctapi.ftqq.com/' + sckey + '.send?title=成功报备&desp=学号' +
str(userName) + '成功报备',
safe='/:?=&')) as response:
response = json.loads(response.read().decode('utf-8'))
if response['code'] == 0 and response['data']['error'] == 'SUCCESS':
print('微信通知成功!')
else:
print(str(response['errno']) + ' error: ' + response['errmsg'])
def run(driver, userName, password, campus, reason, detail, destination, track,
habitation, district, street, capture, path, wechat, sckey):
login(driver, userName, password)
print('=================================')
go_to_application_out(driver)
fill_out(driver, campus, reason, detail, destination, track)
print('=================================')
go_to_application_in(driver, userName, password)
fill_in(driver, campus, reason, detail, habitation, district, street)
print('=================================')
if capture:
screen_capture(driver, path)
print('=================================')
if wechat:
wechat_notification(userName, sckey)
print('=================================')
print('可以愉快的玩耍啦!\n')
if __name__ == '__main__':
pass