import os
import time
import random
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

def check_browser():
    # 检查是否已经打开了浏览器
    try:
        options = Options()
        options.add_experimental_option('debuggerAddress', '127.0.0.1:9527')
        browser = webdriver.Chrome(options=options)
        browser.execute_script('return true')
        return True
    except:
        return False

if __name__ == '__main__':
    if not check_browser():
        os.system(r'start chrome --remote-debugging-port=9527 --user-data-dir="D:\评阅用"')

    options = Options()
    options.add_experimental_option('debuggerAddress', '127.0.0.1:9527')
    browser = webdriver.Chrome(options=options)

    while True:
        for handle in browser.window_handles:
            browser.switch_to.window(handle)
            if '内蒙古开放大学' in browser.title:
                print('用户登录成功!')
                break
        else:
            time.sleep(1)
            continue
        break

    while True:
        try:
            score_box = 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)))
            browser.find_element(By.CLASS_NAME, 'nav-text').click()  # 点击提交按钮
            time.sleep(1)
            while True:
                try:
                    toast_message = browser.find_element(By.CLASS_NAME, 'toast-message')
                    if toast_message.is_displayed():
                        time.sleep(1)
                    else:
                        break
                except:
                    break
        except:
            continue
        else:
            if '已到最后' in browser.page_source:
                print('已到最后,暂停程序')
                break
            time.sleep(1)

代码功能:

  1. 检查浏览器: 检查是否已打开 Chrome 浏览器,并启用了远程调试端口。
  2. 连接浏览器: 连接到已打开的 Chrome 浏览器。
  3. 等待登录: 等待用户登录到目标网站(例如,内蒙古开放大学)。
  4. 自动评分:
    • 循环查找页面上的评分框(class 属性为 'score-box')。
    • 清空评分框的内容。
    • 生成 94 到 99 之间的随机数作为分数。
    • 将随机数输入到评分框中。
    • 点击提交按钮(class 属性为 'nav-text')。
    • 等待评分提交成功提示消息(class 属性为 'toast-message')消失。
  5. 结束循环: 如果页面上出现 '已到最后' 的提示,则停止脚本运行。

使用方法:

  1. 安装 Python 和 Selenium 库。
  2. 下载 ChromeDriver 并将其路径添加到系统环境变量中。
  3. 打开 Chrome 浏览器,并在地址栏中输入 chrome://version/ 找到 “个人资料路径”。
  4. 将代码中的 'D:\\评阅用' 替换为你的 Chrome 用户数据目录路径。
  5. 运行脚本。

注意:

  • 该脚本仅适用于特定网站和页面布局。
  • 使用自动化脚本进行评分可能违反某些网站的服务条款,请谨慎使用。
  • 该脚本仅供学习和参考,请勿用于非法用途。
Python Selenium 自动化评分脚本:模拟人工评分,提高评分效率

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

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