利用selenium自动填写南邮正方的教学质量评价

前言

这几天,南邮正方开放了教学质量评价的接口,苦于一个一个选择很麻烦,花了点时间写了个脚本进行自动填写。主要利用了Python的selenium库模拟浏览器进行自动填写。

转载请注明出处 ******

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
"""
-*-author : QzmVc1 -*-
"""
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import Select
# 显示等待
from selenium.webdriver.support.wait import WebDriverWait
# 设置等待执行语句
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from PIL import Image
from urllib.request import urlretrieve
from os.path import dirname,join
import random

USERNAME = input("请输入正方账号:")
PASSWORD = input("请输入正方密码:")

# 登陆正方
driver = webdriver.Chrome()
driver.get('http://jwxt.njupt.edu.cn/')
# 输入账号
driver.find_element_by_id('txtUserName').clear()
driver.find_element_by_id('txtUserName').send_keys(USERNAME)

# 密码处理
driver.find_element_by_id('Textbox1').click()
driver.find_element_by_xpath('//*[@id="form1"]/div/div[1]/h2/img').click()
driver.find_element_by_id('TextBox2').clear()
driver.find_element_by_id('TextBox2').send_keys(PASSWORD)
# path = join(dirname(__file__),'save.png')
# urlretrieve('http://jwxt.njupt.edu.cn/CheckCode.aspx',path)
# im = Image.open(path)
# im.show()
# 手动输入验证码
cap = input("请输入验证码:")
driver.find_element_by_id('txtSecretCode').send_keys(cap)
# WebDriverWait(driver,10).until(
# EC.presence_of_element_located(
# (By.ID,'TextBox2'),
# )
# )
driver.find_element_by_id('Button1').click()
print("------------南邮正方系统自动登陆成功!------------")
try:
driver.switch_to.alert.accept()
except:
pass
print("------------开始自动填写教学质量评价!------------\n...")
length1 = len(driver.find_elements_by_xpath('//*[@id="headDiv"]/ul/li[3]/ul/li'))

for k in range(1,3):
for i in range(1,length1+1):
drop_down = driver.find_element_by_xpath('//*[@id="headDiv"]/ul/li[3]/a/span')
action = ActionChains(driver)
action.move_to_element(drop_down).perform()
action.move_by_offset(0,i*20).click().perform()
try:
driver.switch_to.alert.accept()
except:
pass
driver.switch_to.frame('iframeautoheight')
length1_ = len(driver.find_elements_by_xpath('//*[@id="DataGrid1"]/tbody/tr'))-1
if k == 1:
for j in range(2,length1_+1):
Select(driver.find_element_by_id('DataGrid1__ctl{0}_JS1'.format(j))).select_by_value("完全认同")
Select(driver.find_element_by_id('DataGrid1__ctl{0}_JS1'.format(length1_+1))).select_by_value("相对认同")
driver.find_element_by_name('Button1').click()
if i == length1:
driver.find_element_by_name('Button2').click()
try:
driver.switch_to.alert.accept()
except:
pass
print("...\n")
else:
for j in range(2, length1_ + 1):
Select(driver.find_element_by_id('DataGrid1__ctl{0}_JS1'.format(j))).select_by_value("好")
Select(driver.find_element_by_id('DataGrid1__ctl{0}_JS1'.format(length1_ + 1))).select_by_value("较好")
if i != length1:
driver.find_element_by_name('Button1').click()
else:
driver.find_element_by_name('Button1').click()
try:
driver.switch_to.alert.accept()
except:
pass
driver.find_element_by_name('Button2').click()
try:
driver.switch_to.alert.accept()
except:
pass
print("...\n")
driver.switch_to.default_content()

driver.quit()

print("------------教学质量评价已全部自动填写完毕!------------")


注意点1:南邮正方密码的send_keys处理

注意点2:iframe框架的处理

注意点3:弹框的异常处理

注意点4: 程序的普适性和鲁棒性