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

发布于:2024-05-10 ⋅ 阅读:(28) ⋅ 点赞:(0)

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

重难点:

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

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

前置知识:

python--selenium模块的操作使用

前端的html代码

验证码自动化操作

Chrome  &   Chromedriver  : Chrome for Testing availability

验证码借助第三方平台:

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

实现代码:

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

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)