Python自动评分系统:提升效率的利器
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
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException
class AutoScore100:
def __init__(self):
# ... (现有代码)
self.forum_thread = None # 添加论坛打分线程
# ... (现有代码)
self.start_forum_button = tk.Button(score_frame, text='论坛打分', command=self.start_forum_score)
self.start_forum_button.pack(side=tk.LEFT, padx=20)
# ... (现有代码)
# ... (现有代码)
def start_program(self):
# ... (现有代码)
self.start_forum_button.config(state='normal') # 启用论坛打分按钮
# ... (现有代码)
def start_forum_score(self):
if not self.running:
return
if self.forum_thread and self.forum_thread.is_alive():
return
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 self.running:
if self.paused:
time.sleep(0.5)
continue
try:
# 查找打分输入框元素
score_input = self.browser.find_element(By.CSS_SELECTOR, 'input[ng-model="currentEnrollment.forum_score.display_score"]')
# 查找'暂无'元素
try:
self.browser.find_element(By.CLASS_NAME, 'blank-message')
self.update_text_box('该学生未参与论坛,跳过打分。')
except NoSuchElementException:
# 未找到'暂无'元素,说明需要打分
score = random.randint(94, 99)
score_input.clear()
score_input.send_keys(str(score))
self.update_text_box(f'论坛评分: {score}')
time.sleep(1)
# 点击下一个学生按钮
try:
next_student_button = self.browser.find_element(By.CSS_SELECTOR, 'i[ng-click="goToNextStudent()"]')
next_student_button.click()
time.sleep(2)
except ElementClickInterceptedException:
self.update_running_label('已到最后,请手动保存后重新开始!')
break
except NoSuchElementException:
self.update_text_box('未找到评分元素,请确认页面加载完成。')
time.sleep(2)
continue
except Exception as e:
print(e)
self.update_running_label('论坛打分失败,请重试')
finally:
self.start_forum_button.config(state='normal')
self.stop_score_button.config(state='disabled')
self.pause_button.config(state='disabled')
# ... (现有代码)
def score_program_100(self):
# ... (现有代码)
finally:
# ... (现有代码)
self.start_forum_button.config(state='normal') # 启用论坛打分按钮
# ... (现有代码)
def score_program(self):
# ... (现有代码)
finally:
# ... (现有代码)
self.start_forum_button.config(state='normal') # 启用论坛打分按钮
# ... (现有代码)
def update_running_label(self, text):
self.running_label.config(text=text)
# ... (现有代码)
if __name__ == '__main__':
auto_score_100 = AutoScore100()
auto_score_100.run()
原文地址: http://www.cveoy.top/t/topic/f2cQ 著作权归作者所有。请勿转载和采集!