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)
相关推荐
小白银子3 小时前
零基础从头教学Linux(Day 52)
linux·运维·服务器·python·python3.11
摇滚侠3 小时前
Spring Boot 3零基础教程,WEB 开发 静态资源默认配置 笔记27
spring boot·笔记·后端
Y42583 小时前
本地多语言切换具体操作代码
前端·javascript·vue.js
AAA小肥杨4 小时前
基于k8s的Python的分布式深度学习训练平台搭建简单实践
人工智能·分布式·python·ai·kubernetes·gpu
LXS_3574 小时前
Day 05 C++ 入门 之 指针
开发语言·c++·笔记·学习方法·改行学it
速易达网络6 小时前
Bootstrap 5 响应式网站首页模板
前端·bootstrap·html
etsuyou6 小时前
js前端this指向规则
开发语言·前端·javascript
lichong9516 小时前
Android studio 修改包名
android·java·前端·ide·android studio·大前端·大前端++
cai_huaer6 小时前
BugKu Web渗透之 cookiesWEB
前端·web安全
lichong9516 小时前
Git 检出到HEAD 再修改提交commit 会消失解决方案
java·前端·git·python·github·大前端·大前端++