Python 自动化脚本:内蒙古开放大学评分系统自动评分
Python 自动化脚本:内蒙古开放大学评分系统自动评分
本脚本使用 Python 和 Selenium 库,模拟用户操作,自动为内蒙古开放大学评分系统中的学生评分。
代码实现
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
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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)))
score_input.send_keys(Keys.ENTER)
time.sleep(1)
nav_text = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'nav-text')))
nav_text.click()
WebDriverWait(browser, 10).until_not(EC.presence_of_element_located((By.CLASS_NAME, 'toast-message')))
except:
continue
if '已到最后' in browser.page_source:
time.sleep(10)
break
代码解释
- 导入必要的库:
os,time,random,selenium,webdriver,Options,By,Keys,WebDriverWait,expected_conditions - 检查浏览器是否已打开:
check_browser()函数用于检查是否已打开 Chrome 浏览器并连接到调试端口 9527。 - 启动浏览器:如果浏览器未打开,则使用
os.system()命令启动 Chrome 浏览器,并指定调试端口和用户数据目录。 - 连接到浏览器:使用
Options()和webdriver.Chrome()创建一个 Chrome 浏览器实例,并连接到调试端口。 - 等待登录成功:循环遍历所有窗口句柄,找到标题包含
内蒙古开放大学的窗口,即登录成功。 - 循环评分:
- 找到
score-box元素,并获取其中的输入框元素。 - 清除输入框内容,并输入 94-99 之间的随机数。
- 点击
nav-text元素。 - 等待
toast-message元素消失。 - 检查页面源代码是否包含
已到最后字样,如果包含,则停止循环。
- 找到
注意
- 脚本需要安装 Selenium 库。
- 脚本需要根据具体的评分系统页面进行调整。
- 脚本仅供参考,请勿用于任何非法目的。
其他
- 使用
WebDriverWait和expected_conditions可以提高脚本的稳定性。 - 可以根据需要调整代码逻辑和评分范围。
免责声明
本脚本仅供学习参考,请勿用于任何非法目的。作者对任何由于使用本脚本而造成的损失不负任何责任。
原文地址: https://www.cveoy.top/t/topic/fYxp 著作权归作者所有。请勿转载和采集!