selenium解放双手--记某电力学校的刷课脚本

免责声明:本文仅做技术交流与学习...

重难点:

1-对目标网站的html框架具有很好的了解,定位元素,精准打击.

2-自动化过程中窗口操作的转换.

前置知识:

python--selenium模块的操作使用

前端的html代码

验证码自动化操作

Chrome & Chromedriver : Chrome for Testing availability

验证码借助第三方平台:

复制代码
登录超级鹰网站 处理 字符串验证码:
复制代码
用户登录-超级鹰验证码识别代答题平台

实现代码:

未优化...(没对象版)

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from chaojiying import Chaojiying_Client
chrome_path = "D:\\Chrome\\chrome-win64\\chrome-win64\\chrome.exe"  # Chrome浏览器的路径
chrome_driver_path = "../chromedriver-win64/chromedriver.exe"  # Chrome WebDriver的路径

chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = chrome_path

service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
# 目标网址:
driver.get("https://elearning.sepcedu.com/login")
time.sleep(4)
# 输入账号
driver.find_element(By.CLASS_NAME, 'el-input__inner').send_keys('你的账号')
time.sleep(2)
# 输入密码
driver.find_element(By.XPATH, '//input[@type="password"]').send_keys('你的密码')
time.sleep(2)
# 输入验证码操作:
driver.find_element(By.XPATH, '//img').screenshot('../img/logo.png')
chaojiying = Chaojiying_Client('账号', '密码', '唯一ID')
im = open('../img/logo.png', 'rb').read()
code = chaojiying.PostPic(im, 3004)['pic_str']    #数字为验证码的类型.
driver.find_element(By.XPATH, '//input[@placeholder="请输入验证码"]').send_keys(code)
time.sleep(1)
# 点击登录
driver.find_element(By.XPATH, '//button').click()
time.sleep(2.5)

# 进入我的学习:
driver.find_element(By.XPATH, '//li[@class="el-menu-item"][1]').click()
time.sleep(1)
# 获取窗口视图
start_window = driver.current_window_handle
# 获取到所有课程的列表
div_s = driver.find_elements(By.XPATH, '//div[@class="courseItem"]')
# 循环课程,单个点击进入:
for div in div_s[6:]:    ########注意,这里的循环遍历自己改.
    pro = div.find_element(By.XPATH, './/div[@class="el-progress-bar__innerText"]/span')
    # 完成度判断
    if pro.text != "100%":
        print(pro.text)
        # 进入课程学习
        an = div.find_element(By.XPATH, './/div[@class="enterCourse"]')
        an.click()
        time.sleep(8)
        # ----------------------------------------------------------
        # 转换操作窗口:
        win = driver.window_handles
        print(win)
        time.sleep(5)
        driver.switch_to.window(win[1])
        time.sleep(2)
        # 先全部展开
        try:
            # 展开
            zk = driver.find_element(By.XPATH, '//a[@class="section-collapsemenu collapsed"]')
            zk.click()
            time.sleep(1)
        except Exception as e:
            print("发生了其它异常", e)
        finally:
            # 按钮:
            buttons = driver.find_elements(By.XPATH, '//button[@class="btn btn-outline-secondary btn-sm text-nowrap"]')
            for button in buttons:
                button.click()
                time.sleep(0.5)
            time.sleep(1)
            # 视频,PDF,文档等等:
            links = driver.find_elements(By.XPATH, '//ul[@data-for="cmlist"]//li[@data-indexed="true"]//a[@class=" aalink stretched-link"]')
            href_list = [link.get_attribute('href') for link in links if link.get_attribute('href').startswith('https')]
            # print(href_list)
            for href in href_list:
                print(href+'成功')
                driver.get(href)
                time.sleep(2)
            # 循环后切换页面
            driver.close()
            driver.switch_to.window(win[0])
    else:
        pass
print("圆满结束")
print("温馨提示:需要自己提交and学习的作业还需要自己去做欧~~~")
time.sleep(30)
相关推荐
PineappleCoder3 小时前
性能数据别再瞎轮询了!PerformanceObserver 异步捕获 LCP/CLS,不卡主线程
前端·性能优化
PineappleCoder3 小时前
告别字体闪烁 / 首屏卡顿!preload 让关键资源 “高优先级” 提前到
前端·性能优化
晓梦.4 小时前
Vue3学习笔记
笔记·学习
路边草随风4 小时前
milvus向量数据库使用尝试
人工智能·python·milvus
m0_471199634 小时前
【vue】通俗详解package-lock文件的作用
前端·javascript·vue.js
newobut4 小时前
vscode远程调试python程序,基于debugpy库
vscode·python·调试·debugpy
思成不止于此4 小时前
【MySQL 零基础入门】DQL 核心语法(二):表条件查询与分组查询篇
android·数据库·笔记·学习·mysql
GIS之路4 小时前
GDAL 读取KML数据
前端
今天不要写bug5 小时前
vue项目基于vue-cropper实现图片裁剪与图片压缩
前端·javascript·vue.js·typescript