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)
WebDriverWait(browser, 5).until(EC.invisibility_of_element_located((By.CLASS_NAME, 'toast-message')))
browser.find_element(By.CLASS_NAME, 'nav-text').click()
except:
time.sleep(1)
continue
该脚本使用 Python 和 Selenium 库实现内蒙古开放大学网页评分自动化,包括以下功能:
- 检查浏览器是否已经打开,如果没有则启动浏览器并设置调试端口
- 等待用户登录成功
- 在网页上找到 class 属性为 'score-box' 的元素,清除该元素输入框的内容,并填写 94-99 的随机数
- 等待 'toast-message' 元素消失后,点击 'nav-text' 元素,提交评分
使用说明:
- 安装必要的库:
pip install selenium - 下载 Chrome 浏览器驱动程序,并将其放到系统环境变量 PATH 中
- 将 'D:\评阅用' 替换成你的用户数据目录
- 运行脚本
注意事项:
- 该脚本仅供学习参考,请勿用于任何非法目的
- 请确保你已获得相关网站的授权,方可使用该脚本
- 由于网站结构可能随时变化,脚本可能需要进行调整才能正常运行
原文地址: https://www.cveoy.top/t/topic/fYvW 著作权归作者所有。请勿转载和采集!