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_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_input = self.browser.find_element(By.CSS_SELECTOR, 'input[ng-model="currentEnrollment.forum_score.display_score"]')
                        break
                    except:
                        pass
                    time.sleep(3)

                # 判断是否需要打分
                try:
                    blank_message = self.browser.find_element(By.CLASS_NAME, 'blank-message')
                    if blank_message.text == '暂无':
                        self.update_text_box('本次论坛不需要打分')
                        continue
                except:
                    pass

                # 填入分值
                score_input.clear()
                score = random.randint(94, 99)
                score_input.send_keys(str(score))
                time.sleep(1.5)

                # 点击保存按钮
                save_button = self.browser.find_element(By.CSS_SELECTOR, 'input[ng-blur="saveScore(currentEnrollment)"]')
                save_button.click()
                time.sleep(2.5)

                self.update_text_box(f'本次论坛分值为:{score}')
                # 点击下一个学生按钮
                try:
                    next_student_button = self.browser.find_element(By.CSS_SELECTOR, 'i[ng-click="goToNextStudent()"]')
                    next_student_button.click()
                    time.sleep(3)
                except:
                    self.update_running_label('已到最后,请手动保存后重新开始!')
                    break
                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()

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

免费AI点我,无需注册和登录