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

# 查找待批改元素并统计数量
not_marked_elements = driver.find_elements_by_xpath('//div[@class="status not-marked ng-scope" and @ng-if="subjectList[getIndex(row, column)] and subjectList[getIndex(row, column)].score == null"]/span[@class="text ng-binding" and @ng-bind="notMarked"]')
not_marked_count = len(not_marked_elements)
print('待批改数量:', not_marked_count)

本脚本使用 Selenium 自动化框架,查找内蒙古开放大学平台中待批改的作业数量,并打印结果。

步骤:

  1. 获取浏览器驱动: 使用 get_driver() 函数获取 Chrome 浏览器驱动,并检查是否已经打开浏览器,如果已打开,则使用调试器模式连接已有浏览器,否则新建一个浏览器实例。
  2. 找到目标标签页: 在所有打开的标签页中查找标题包含'内蒙古开放大学'的标签页,如果找到,则打印'登录成功',并结束循环。
  3. 查找待批改元素: 使用 find_elements_by_xpath() 方法查找所有符合条件的待批改元素(div 元素,包含 status not-marked ng-scope 类,且 ng-if 属性为 subjectList[getIndex(row, column)] and subjectList[getIndex(row, column)].score == null),并获取其子元素 span 的数量,即待批改作业数量。
  4. 打印结果: 将待批改作业数量打印到控制台。

注意:

  • 该脚本需要安装 Selenium 库。
  • 脚本中的 D:\评阅用 路径需要根据实际情况进行修改。
  • 脚本中的 XPath 表达式可能需要根据网页结构进行调整。
  • 该脚本仅供参考,具体实现方法可能因网页结构和功能的不同而有所差异。
Selenium 自动化脚本:统计内蒙古开放大学待批改作业数量

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

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