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)
相关推荐
wdfk_prog10 分钟前
[Linux]学习笔记系列 -- [kernel][time]alarmtimer
linux·笔记·学习
小志biubiu11 分钟前
【Linux】Ext系列文件系统
linux·服务器·c语言·经验分享·笔记·ubuntu·操作系统
charlie11451419136 分钟前
现代 Python 学习笔记:Statements & Syntax
笔记·python·学习·教程·基础·现代python·python3.13
一只小透明啊啊啊啊2 小时前
Java Web 开发的核心组件:Servlet, JSP,Filter,Listener
java·前端·servlet
你的人类朋友2 小时前
设计模式有哪几类?
前端·后端·设计模式
Yeats_Liao2 小时前
Go Web 编程快速入门 10 - 数据库集成与ORM:连接池、查询优化与事务管理
前端·数据库·后端·golang
安冬的码畜日常3 小时前
【JUnit实战3_10】第六章:关于测试的质量(上)
测试工具·junit·单元测试·测试覆盖率·1024程序员节·junit5
啃火龙果的兔子3 小时前
前端八股文react篇
前端·react.js·前端框架
打小就很皮...3 小时前
React 实现 i18next 中英文切换集成
前端·react.js·i18next
拉不动的猪3 小时前
函数组件和异步组件
前端·javascript·面试