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)
相关推荐
跃渊Yuey9 分钟前
【Linux】线程同步与互斥
linux·笔记
数据安全科普王9 分钟前
从 HTTP/1.1 到 HTTP/3:协议演进如何改变 Web 性能?
网络·其他
葱明撅腚10 分钟前
利用Python挖掘城市数据
python·算法·gis·聚类
We་ct12 分钟前
LeetCode 36. 有效的数独:Set实现哈希表最优解
前端·算法·leetcode·typescript·散列表
Serendipity_Carl17 分钟前
1637加盟网数据实战(数分可视化)
爬虫·python·pycharm·数据可视化·数据清洗
流㶡19 分钟前
网络爬虫之requests.get() 之爬取网页内容
python·数据爬虫
llilian_1621 分钟前
信号发生器 多通道多功能脉冲信号发生器应用解决方案 多功能脉冲发生器
功能测试·单片机·嵌入式硬件·测试工具
AI视觉网奇23 分钟前
FBX AnimSequence] 动画长度13与导入帧率30 fps(子帧0.94)不兼容。动画必须与帧边界对齐。
笔记·学习·ue5
weixin_3954489124 分钟前
main.c_cursor_0129
前端·网络·算法
yuankoudaodaokou29 分钟前
高校科研新利器:思看科技三维扫描仪助力精密研究
人工智能·python·科技