自动批改内蒙古开放大学作业脚本
from selenium.webdriver.chrome.options import Options from selenium import webdriver import time import os import random
def get_driver(): # 检查是否已经打开浏览器 browser_opened = False for handle in webdriver.Chrome().window_handles: browser_opened = True break
# 创建浏览器实例或在已有浏览器中操作
if browser_opened:
options = Options()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
else:
os.system(r'start chrome --remote-debugging-port=9222 --user-data-dir="D:\评阅用"')
options = Options()
options.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
driver = webdriver.Chrome(options=options)
return driver
driver = get_driver()
找到有'内蒙古开放大学'字样的标签页
while True: for handle in driver.window_handles: driver.switch_to.window(handle) if '内蒙古开放大学' in driver.title: print('登录成功') break else: time.sleep(3) continue break
循环打分
while True: # 查找待批改的元素 while True: not_marked_elements = driver.find_elements('css selector', 'span.text.ng-binding[ng-bind='notMarked']') if len(not_marked_elements) > 0: break else: time.sleep(3)
if len(not_marked_elements) == 1:
# 查找该元素对应的题号和分值
number_elements = driver.find_elements('css selector', 'span.ng-binding.ng-scope[ng-if='getIndex(row, column) < subjectList.length']')
score_elements = driver.find_elements('class name', 'summary-sub-title')
if len(number_elements) == len(score_elements):
# 对分值减分
score_list = []
for score_element in score_elements:
score = int(''.join(filter(str.isdigit, score_element.text)))
score -= random.randint(1, 2)
score_list.append(score)
print(f'分值为:{score_list}')
# 填写分数
for i, score in enumerate(score_list):
print(f'第{i + 1}题分值为:{score}')
score_input = driver.find_elements('css selector', 'input[name='score']')[i]
score_input.clear()
score_input.send_keys(str(score))
time.sleep(1.5) # 等待自动保存
print(f'第{i + 1}题已打分:{score}')
else:
print('题号和分值数量不一致')
elif len(not_marked_elements) > 1:
for not_marked_element in not_marked_elements:
# 查找该元素对应的题号和分值
number_elements = not_marked_element.find_elements('css selector', 'span.ng-binding.ng-scope[ng-if='getIndex(row, column) < subjectList.length']')
score_elements = not_marked_element.find_elements('class name', 'summary-sub-title')
if len(number_elements) == len(score_elements):
# 对分值减分
score_list = []
for score_element in score_elements:
score = int(''.join(filter(str.isdigit, score_element.text)))
score -= random.randint(1, 2)
score_list.append(score)
print(f'分值为:{score_list}')
# 填写分数
for i, score in enumerate(score_list):
print(f'第{i + 1}题分值为:{score}')
score_input = not_marked_element.find_elements('css selector', 'input[name='score']')[i]
score_input.clear()
score_input.send_keys(str(score))
time.sleep(1.5) # 等待自动保存
print(f'第{i + 1}题已打分:{score}')
else:
print('题号和分值数量不一致')
else:
print('没有待批改的元素')
time.sleep(3)
# 查找下一个学生按钮并点击
for i in range(3):
try:
next_student_button = driver.find_element('css selector', 'span.icon-student-right-narrow.clickable')
next_student_button.click()
time.sleep(3) # 等待页面加载完成
break
except:
time.sleep(3)
continue
else:
print('已到最后一个学生')
break
原文地址: https://www.cveoy.top/t/topic/fZnn 著作权归作者所有。请勿转载和采集!