Python自动化评分工具:Selenium实现高效作业批改
import os
import random
import time
import tkinter as tk
from threading import Thread
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
class AutoScore100:
def __init__(self):
self.running = False
self.paused = False
self.browser = None
self.thread = None
self.score_thread = None
self.forum_thread = None
self.root = tk.Tk()
self.root.title('评分机器人1.2')
self.root.geometry('300x500')
# 创建按钮容器
button_frame = tk.Frame(self.root)
button_frame.pack(pady=10)
# 创建“开始运行”和“停止运行”按钮
self.start_button = tk.Button(button_frame, text='开始运行', command=self.start_program)
self.start_button.pack(side=tk.LEFT, padx=20)
self.stop_score_button = tk.Button(button_frame, text='停止运行', command=self.stop_program)
self.stop_score_button.pack(side=tk.LEFT)
# 创建“百分制打分”、“小分值打分”和“论坛打分”按钮
score_frame = tk.Frame(self.root)
score_frame.pack(pady=10)
self.start_score_button = tk.Button(score_frame, text='百分值打分', command=self.start_score_100)
self.start_score_button.pack(side=tk.LEFT, padx=20)
self.start_score_button_2 = tk.Button(score_frame, text='小分值打分', command=self.start_score)
self.start_score_button_2.pack(side=tk.LEFT)
self.start_forum_button = tk.Button(score_frame, text='论坛打分', command=self.start_forum_score)
self.start_forum_button.pack(side=tk.LEFT, padx=20)
# 创建“暂停打分”按钮和状态标签
self.pause_button = tk.Button(self.root, text='暂停打分', command=self.pause_program)
self.pause_button.pack(pady=10)
self.running_label = tk.Label(self.root, text='')
self.running_label.pack(pady=10)
# 创建提示标签
self.text_box = tk.Text(self.root, height=50, state='disabled')
self.text_box.pack(pady=50)
# 创建浏览器实例
options = Options()
options.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
try:
self.browser = webdriver.Chrome(options=options)
except:
pass
def start_program(self):
# ... (其他方法代码保持不变)
def start_forum_score(self):
if not self.running:
return
if self.forum_thread and self.forum_thread.is_alive():
return
self.start_score_button.config(state='disabled')
self.start_score_button_2.config(state='disabled')
self.start_forum_button.config(state='disabled') # 禁用论坛打分按钮
self.stop_score_button.config(state='normal')
self.update_running_label('论坛打分中...')
self.forum_thread = Thread(target=self.forum_score_program)
self.forum_thread.start()
def forum_score_program(self):
try:
# 循环打分
while True:
if not self.running:
break
if self.paused:
continue
# 等待打分元素出现
while True:
try:
score_element = self.browser.find_element(By.XPATH, '//input[@ng-model="currentEnrollment.forum_score.display_score"]')
break
except:
pass
time.sleep(3)
# 判断是否已经打过分
try:
blank_message_element = self.browser.find_element(By.XPATH, '//span[text()="暂无"]')
self.update_text_box('该学生已经打过分了\n')
# 点击下一个学生按钮
next_student_element = self.browser.find_element(By.XPATH, '//i[@ng-click="goToNextStudent()"]')
next_student_element.click()
time.sleep(3)
self.update_running_label('论坛打分中...')
continue
except:
pass
# 填写分值
score = random.randint(94, 99)
score_element.clear() # 清空可能存在的默认值
score_element.send_keys(str(score))
time.sleep(1.5)
# 提交分值 (模拟按键操作触发ng-blur事件)
score_element.send_keys(Keys.TAB) # 将焦点移开输入框
time.sleep(2.5)
self.update_text_box(f'本次论坛分值为:{score}\n')
# 点击下一个学生
next_student_element = self.browser.find_element(By.XPATH, '//i[@ng-click="goToNextStudent()"]')
if 'disabled' in next_student_element.get_attribute('class'):
print('已经到最后一个学生')
self.update_running_label('已到最后,请手动保存后重新开始!')
break
else:
next_student_element.click()
time.sleep(3)
self.update_running_label('论坛打分中...')
except Exception as e:
print(e)
self.update_running_label('论坛打分失败,请重试')
finally:
self.start_score_button.config(state='normal')
self.start_score_button_2.config(state='normal')
self.start_forum_button.config(state='normal') # 启用论坛打分按钮
self.stop_score_button.config(state='disabled')
self.pause_button.config(state='disabled')
# ... (其他方法代码保持不变)
if __name__ == '__main__':
auto_score_100 = AutoScore100()
auto_score_100.run()
代码修改说明:
-
新增
start_forum_score
方法和按钮:- 在
__init__
方法中添加一个名为self.start_forum_button
的按钮,用于启动论坛打分功能。 - 创建
start_forum_score
方法,用于处理按钮点击事件,创建并启动论坛打分线程self.forum_thread
。
- 在
-
实现
forum_score_program
方法:- 使用
//input[@ng-model='currentEnrollment.forum_score.display_score']
XPATH 表达式定位论坛打分输入框。 - 使用
//span[text()='暂无']
XPATH 表达式判断是否已经打过分。 - 使用
//i[@ng-click='goToNextStudent()']
XPATH 表达式定位下一个学生按钮。 - 使用
score_element.send_keys(Keys.TAB)
模拟键盘操作触发ng-blur
事件,提交分值。
- 使用
-
其他修改:
- 在
forum_score_program
方法中,使用score_element.clear()
清空可能存在的默认值,确保分值正确填写。 - 在
start_forum_score
和forum_score_program
方法中,根据线程状态启用或禁用论坛打分按钮,防止重复启动线程。
- 在

原文地址: http://www.cveoy.top/t/topic/f2ci 著作权归作者所有。请勿转载和采集!