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: # 统计待批改的元素数量和题号 not_marked_elements = driver.find_elements('css selector', 'div.status.not-marked.ng-scope') not_marked_nums = [] for element in not_marked_elements: num_element = element.find_element('css selector', 'div.subject-number span.ng-binding.ng-scope') not_marked_nums.append(num_element.text) print(f'待批改的元素有{len(not_marked_elements)}个,题号分别为{not_marked_nums}')

# 在网页内查找所有class属性为summary-sub-title的元素,并提取其中的数字
while True:
    
    elements = driver.find_elements('class name', 'summary-sub-title')
    if len(elements) > 0:
        break
    else:
        time.sleep(3)

if len(elements) == 1:
    # 对该数字减分
    score = int(''.join(filter(str.isdigit, elements[0].text)))
    score -= random.randint(1, 2)
    print(f'分值为:{score}')

    # 判断是否已经填写了答案
    answer = driver.find_element('class name', 'answer-content')
    if answer.text:
        # 填写分数
        print('可以打分')
        score_input = driver.find_element('css selector', 'input[name='score']')
        score_input.clear()
        score_input.send_keys(str(score))
        time.sleep(1.5)  # 等待自动保存
        print(f'已打分:{score}')
else:
    scores = []
    for element in elements:
        text = element.text
        num = ''.join(filter(str.isdigit, text))
        num = int(num)
        scores.append(num)

    # 随机对2-5个数字减去1-2分
    num_to_reduce = random.randint(2, 5)
    if len(scores) >= num_to_reduce > 0:
        for i in random.sample(range(len(scores)), num_to_reduce):
            scores[i] -= random.randint(1, 2)

    # 打印修改后的分数
    for i, score in enumerate(scores):
        print(f'第{i + 1}题分值为:{score}')

        # 判断是否已经填写了答案
        answer = driver.find_element('class name', 'answer-content')
        if answer.text:
            # 填写分数
            print(f'第{i + 1}题可以打分')
            score_input = driver.find_elements('css selector', 'input[name='score']')[i]
            score_input.clear()
            score_input.send_keys(str(score))
            time.sleep(1)  # 等待自动保存
            print(f'第{i + 1}题已打分:{score}')
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
Selenium 自动化打分脚本 - 内蒙古开放大学评阅系统

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

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