from selenium import webdriver
import time
import pandas as pd
from lib import user_info
from data import data
# 等待提示
def wait_key():
while True:
message = input('请输入“空格键”继续:')
if message == ' ':
break
print('你输入的是 ' + message + '!')
# 判断是否有弹出框
def alert_is_present(browser):
try:
alert = browser.switch_to.alert
print(alert.text)
return alert
except:
return False
browser = webdriver.Chrome()
url = 'http://121.28.25.166/StationWeb/pages/common/frameset.jsp'
browser.get(url)
# 登录
browser.find_element_by_name('j_username_temp').send_keys(user_info.username)
browser.find_element_by_name('j_password').send_keys(user_info.pswd)
browser.find_element_by_name('txtschool').send_keys('1300300')
# browser.find_element_by_name('veryCode').click()
wait_key()
# paper_id_ls = ['7908', '1905']
for d in data:
for paper_id, v in d.items():
handles = browser.window_handles # 获得所有窗口句柄
browser.switch_to.window(handles[-1]) # 取最新的
browser.switch_to.parent_frame() # 返回父框架
browser.switch_to.frame('main') # 定位到main框架
browser.switch_to.frame('Info') # Info
# todo 输入试卷号
time.sleep(3)
browser.find_element_by_name('paperId').clear()
browser.find_element_by_name('paperId').send_keys(paper_id)
browser.find_element_by_name('method').click()
time.sleep(5)
# 点击录入
browser.find_element_by_xpath('/html/body/form/table[2]/tbody/tr[3]/td[10]/a').click()
for stu_num, score in v.items():
handles = browser.window_handles # 获得所有窗口句柄
browser.switch_to.window(handles[-1]) # 取最新的
time.sleep(3)
browser.find_element_by_xpath('/html/body/form/table/tbody/tr[2]/td[4]/input').clear()
browser.find_element_by_xpath('/html/body/form/table/tbody/tr[2]/td[4]/input').send_keys(stu_num)
# 点击查找进入下一页
browser.find_element_by_xpath('/html/body/form/p/input').click()
handles = browser.window_handles # 获得所有窗口句柄
browser.switch_to.window(handles[-1]) # 取最新的
time.sleep(3)
# print(f'当前学生成绩{new_score}')
# 修改成绩
js = f'document.querySelector("body > form > table:nth-child(15) > tbody > tr:nth-child(5) > td > input[type=hidden]:nth-child(2)").value="{str(score)}";'
browser.execute_script(js)
# 点击保存
browser.find_element_by_xpath('/html/body/form/p[1]/input').click()
# 如果有弹出框 点击确定
if alert_is_present(browser):
browser.switch_to.alert.accept()
print('录入成功')
# 写入日志 学号+试卷号+成绩
with open('log.txt', 'a+') as f:
f.write(stu_num + ' ' + paper_id + ' ' + str(score) + '\n')
time.sleep(3)
browser.close()
网友评论