一种基于selenium的模拟登录奥蓝教务系统的方法
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import datetime
import tkinter as tk
import xlrd
# 全局变量
name = ''
password = ''
year = ''
month = ''
day = ''
hour = ''
minute = ''
second = ''
def get_user():
window = tk.Tk()
window.geometry('600x1000')
window.title('用户登录')
name_default = tk.StringVar(window, value=' ')
pw_default = tk.StringVar(window, value=' ')
label = tk.Label(window, text='请输入用户名:', )
label.pack()
name_input = tk.Entry(textvariable=name_default)
name_input.pack()
pw_label = tk.Label(window, text='请输入密码:', )
pw_label.pack()
password_input = tk.Entry(textvariable=pw_default)
password_input.pack()
year_label = tk.Label(window, text='年:', )
year_input = tk.Entry(textvariable=tk.StringVar(window, value='2020'))
month_label = tk.Label(window, text='月:')
month_input = tk.Entry(textvariable=tk.StringVar(window, value='9'))
day_label = tk.Label(window, text='日')
day_input = tk.Entry(textvariable=tk.StringVar(window, value='27'))
hour_label = tk.Label(window, text='时')
hour_input = tk.Entry(textvariable=tk.StringVar(window, value='15'))
minute_label = tk.Label(window, text='分')
minute_input = tk.Entry(textvariable=tk.StringVar(window, value='0'))
second_label = tk.Label(window, text='秒')
second_input = tk.Entry(textvariable=tk.StringVar(window, value='0'))
year_label.pack()
year_input.pack()
month_label.pack()
month_input.pack()
day_label.pack()
day_input.pack()
hour_label.pack()
hour_input.pack()
minute_label.pack()
minute_input.pack()
second_label.pack()
second_input.pack()
def login():
global name
global password
global year
global month
global day
global hour
global minute
global second
name = name_input.get()
password = password_input.get()
year = year_input.get()
month = month_input.get()
day = day_input.get()
hour = hour_input.get()
minute = minute_input.get()
second = second_input.get()
button = tk.Button(window, text='确定', command=login)
button.pack()
button = tk.Button(window, text='退出', command=window.quit)
button.pack()
window.mainloop()
def get_course(file='get_test.xls'):
try:
# 打开Excel文件读取数据
data = xlrd.open_workbook(file)
# 获取第一个工作表
table = data.sheet_by_index(0)
# 获取行数
nrows = table.nrows
# 获取列数
ncols = table.ncols
# 定义excel_list
excel_list = []
for row in range(1, nrows):
for col in range(ncols):
# 获取单元格数据
cell_value = table.cell(row, col).value
# 把数据追加到excel_list中
excel_list.append(cell_value)
return excel_list
except Exception as e:
print(str(e))
def set_time():
startTime = datetime.datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))
# print('Program not starting yet...')
while datetime.datetime.now() < startTime:
time.sleep(1)
# print('Program now starts on %s' % startTime)
# print('Executing...')
def login(pub_li, now_li):
# 登录
url = 'https://isea.sztu.edu.cn/#'
drviver = webdriver.Chrome()
drviver.get(url)
user_name = drviver.find_element_by_id('userAccount')
user_name.send_keys(name)
user_pw = drviver.find_element_by_id('userPassword')
user_pw.send_keys(password)
drviver.find_element_by_id('btnSubmit').click()
time.sleep(2)
# 进入系统
drviver.find_element_by_xpath('/html/body/div[5]/a[1]/div/div[2]').click()
# drviver.find_element_by_xpath('//*[@id="tbKxkc"]/tbody/tr[2]/td[4]/a').click()
# drviver.find_element_by_xpath('/html/body/div[3]/div[2]/center/input').click()
time.sleep(100000)
# 本学期选课
# 跳转到新标签页进行操作
# num = drviver.window_handles
# drviver.switch_to_window(num[1])
# drviver.find_element_by_xpath('//*[@id="topmenu"]/li[2]/a').click()
# time.sleep(1)
# drviver.switch_to_frame('mainFrame')
# print(now_li)
# for now in now_li:
# try:
# cid_input = drviver.find_element_by_id('kcxx')
# cid_input.send_keys(now)
# drviver.find_element_by_xpath('/html/body/div[2]/input[6]').click()
# time.sleep(1)
# selector = drviver.find_element_by_xpath('//*[@id="div_202020211001245"]/a')
# selector.click()
# time.sleep(1)
# al = drviver.switch_to_alert()
# time.sleep(1)
# al.accept()
# time.sleep(1)
# cid_input.send_keys(Keys.CONTROL, 'a')
# cid_input.send_keys(Keys.BACK_SPACE)
# except:
# cid_input.send_keys(Keys.CONTROL, 'a')
# cid_input.send_keys(Keys.BACK_SPACE)
# # 公共课
# print('public start')
# drviver.switch_to.parent_frame()
# drviver.find_element_by_xpath('//*[@id="topmenu"]/li[3]/a').click()
# time.sleep(1)
# drviver.switch_to_frame('mainFrame')
# for pub in pub_li:
# try:
# cid_input = drviver.find_element_by_id('kcxx')
# cid_input.send_keys(pub)
# drviver.find_element_by_xpath('/html/body/div[2]/input[6]').click()
# time.sleep(1)
#
# selector = drviver.find_element_by_xpath('//*[@id="div_202020211000327"]/a')
# selector.click()
# time.sleep(1)
# al = drviver.switch_to_alert()
# time.sleep(1)
# al.accept()
# time.sleep(1)
# cid_input.send_keys(Keys.CONTROL, 'a')
# cid_input.send_keys(Keys.BACK_SPACE)
# except:
# cid_input.send_keys(Keys.CONTROL, 'a')
# cid_input.send_keys(Keys.BACK_SPACE)
if __name__ == '__main__':
get_user()
course_list = get_course() # 获取excel
now_course = [] # 本学期课表
pub_course = [] # 公共课
j = 0
for i in course_list:
j = j + 1
if j % 2 == 0:
pub_course.append(i)
else:
now_course.append(i)
set_time()
login(pub_course, now_course)
print('Success')
仅供参考