Selenium 自动化脚本:统计内蒙古开放大学待批改作业数量
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
# 查找待批改的数量
num_waiting = 0
elements = driver.find_elements_by_xpath('//div[@class="status not-marked ng-scope" and @ng-if="subjectList[getIndex(row, column)] && subjectList[getIndex(row, column)].score == null"]/span[@class="text ng-binding"]')
for element in elements:
if element.text == '待批改':
num_waiting += 1
print('待批改数量:', num_waiting)
该脚本使用 Selenium 自动化框架,实现以下功能:
- 登录内蒙古开放大学平台。
- 查找所有带有'待批改'标签的作业。
- 统计并输出待批改作业的数量。
使用方法:
- 确保已安装 Selenium 库。
- 将脚本中的
D:\评阅用替换为你的 Chrome 用户数据目录路径。 - 运行脚本。
注意:
- 该脚本仅供参考,实际使用时可能需要根据平台界面调整代码。
- 请勿使用该脚本进行任何非法活动。
相关知识:
- Selenium 是一个自动化测试框架,可以用于控制浏览器进行自动化操作。
- Chrome 用户数据目录存储了你的浏览器配置和历史记录等信息。
find_elements_by_xpath方法用于通过 XPath 表达式查找网页元素。switch_to.window方法用于切换浏览器窗口。text属性用于获取网页元素的文本内容。
希望以上内容能够帮助你了解该脚本的功能和使用方法。如果你还有其他问题,请随时提出。
原文地址: https://www.cveoy.top/t/topic/oIkM 著作权归作者所有。请勿转载和采集!