自动评分工具 - 快速高效打分助手
import random # 用于生成随机数 import os # 用于执行系统命令 import time # 用于控制时间 import tkinter as tk # 用于创建 GUI 界面 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 # 打分的线程
# 创建 GUI 界面
self.root = tk.Tk()
self.root.title('自动评分—100')
self.root.geometry('300x300')
# 创建开始运行按钮
self.start_button = tk.Button(self.root, text='开始运行', command=self.start_program)
self.start_button.pack(pady=10)
# 创建停止运行按钮
self.stop_score_button = tk.Button(self.root, text='停止运行', command=self.stop_program)
self.stop_score_button.pack(pady=10)
self.stop_score_button.config(state='disabled')
# 创建开始打分按钮
self.start_score_button = tk.Button(self.root, text='开始打分', command=self.start_score)
self.start_score_button.pack(pady=10)
self.start_score_button.config(state='disabled')
# 创建程序运行状态的标签
self.running_label = tk.Label(self.root, text='')
self.running_label.pack(pady=10)
# 创建暂停打分按钮
self.pause_button = tk.Button(self.root, text='暂停打分', command=self.pause_program)
self.pause_button.pack(pady=10)
self.pause_button.config(state='disabled')
def start_program(self):
if self.running: # 如果程序已经在运行中,则直接返回
return
self.running = True # 标记程序开始运行
self.thread = Thread(target=self.run_program) # 创建程序运行的线程
self.thread.start() # 启动线程
self.start_button.config(state='disabled') # 禁用开始运行按钮
self.start_score_button.config(state='normal') # 启用开始打分按钮
self.pause_button.config(state='normal') # 启用暂停打分按钮
self.stop_score_button.config(state='normal') # 启用停止运行按钮
def start_score(self):
if not self.running: # 如果程序没有在运行中,则直接返回
return
if self.score_thread and self.score_thread.is_alive(): # 如果打分线程已经在运行中,则直接返回
return
self.start_score_button.config(state='disabled') # 禁用开始打分按钮
self.stop_score_button.config(state='normal') # 启用停止运行按钮
self.update_running_label('打分中...') # 更新程序运行状态的标签
self.score_thread = Thread(target=self.score_program) # 创建打分的线程
self.score_thread.start() # 启动线程
def stop_program(self):
self.running = False # 标记程序停止运行
self.paused = False # 标记打分没有被暂停
self.start_button.config(state='normal') # 启用开始运行按钮
self.start_score_button.config(state='disabled') # 禁用开始打分按钮
self.stop_score_button.config(state='disabled') # 禁用停止运行按钮
self.pause_button.config(state='disabled') # 禁用暂停打分按钮
self.update_running_label('已停止运行') # 更新程序运行状态的标签
def pause_program(self):
if not self.running: # 如果程序没有在运行中,则直接返回
return
if self.paused: # 如果打分已经被暂停,则继续打分
self.paused = False
self.pause_button.config(text='暂停打分')
self.start_score_button.config(state='normal')
else: # 如果打分没有被暂停,则暂停打分
self.paused = True
self.pause_button.config(text='继续打分')
self.start_score_button.config(state='disabled')
def update_running_label(self, text):
self.running_label.config(text=text) # 更新程序运行状态的标签
def run_program(self):
os.system(r'start chrome --remote-debugging-port=9527 --user-data-dir="D:\评阅用"') # 启动 Chrome 浏览器
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9527") # 设置 Chrome 浏览器的调试地址
while self.running:
while self.paused: # 如果打分被暂停,则等待一段时间再继续执行
time.sleep(0.5)
self.update_running_label('等待登录...')
try:
if not self.browser: # 如果浏览器对象不存在,则创建浏览器对象
self.browser = webdriver.Chrome(options=options)
else: # 如果浏览器对象已经存在,则判断是否已经登录
while True:
for handle in self.browser.window_handles:
self.browser.switch_to.window(handle)
if '内蒙古开放大学' in self.browser.title:
self.update_running_label('登录成功')
self.start_score_button.config(state='normal')
break
else:
self.update_running_label('未登录')
time.sleep(1)
continue
break
break
except Exception as e:
print(e)
continue
def score_program(self):
try:
while True:
if not self.running: # 如果程序已经停止运行,则退出循环
break
if self.paused: # 如果打分被暂停,则等待一段时间再继续执行
continue
score_box = self.browser.find_element(By.CLASS_NAME, 'score-box') # 找到打分框
score_input = score_box.find_element(By.TAG_NAME, 'input') # 找到打分输入框
score_input.clear() # 清空打分输入框
score_input.send_keys(str(random.randint(94, 99))) # 随机输入一个分数
time.sleep(1)
submit_button = self.browser.find_element(By.CLASS_NAME, 'nav-text') # 找到提交按钮
submit_button.click() # 点击提交按钮
time.sleep(2)
if '已到最后' in self.browser.page_source: # 如果已经到最后一页,则停止打分
self.update_running_label('已到最后,请手动保存后重新开始!')
break
except Exception as e:
print(e)
self.update_running_label('打分失败,请重试')
finally:
self.start_score_button.config(state='normal') # 启用开始打分按钮
self.stop_score_button.config(state='disabled') # 禁用停止运行按钮
self.pause_button.config(state='disabled') # 禁用暂停打分按钮
def run(self):
self.root.mainloop() # 运行 GUI 程序
if name == 'main': auto_score_100 = AutoScore100() auto_score_100.run() # 启动程序
原文地址: https://www.cveoy.top/t/topic/fY2L 著作权归作者所有。请勿转载和采集!