刷网课seleinum 小程序

遇到的问题看的参考文章如下
知乎 selenium iframe处理
CSDN Selenium在定位的class含有空格的复合类的解决办法
Python+Selenium笔记(九):操作警告和弹出框
Python3 错误和异常的使用
selenium之 玩转鼠标键盘操作(ActionChains)
selenium 浏览器导航操作
根据ManicTime显示,大约花了10小时左右

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import re
import time
studentId = *******
password = '*******'
kaishi = '1.1'
jieshu = '11.1'
def logon(driver):
    driver.get(r'********')
    driver.find_element_by_class_name('loginBtn').click()
    driver.find_element_by_name('uname').send_keys(studentId)
    driver.find_element_by_name('password').send_keys(password)
    #input()
    #driver.switch_to.frame('frame_content')
    #driver.find_element_by_class_name('clearfix').click()
    #time.sleep(2)

def enterCourse(driver,courseUrl):
    driver.get(courseUrl)

def acquireUnits(driver,startId,endId):   #获取所刷课程url,返回一个列表
    units = driver.find_elements_by_class_name('units')
    unitsDict = {}
    a = 0
    for i in units:
        a = a+1
        z = 0
        print(i.text.split('\n')[0:2])
        for i_1 in i.find_elements_by_class_name('articlename'):
            z += 1
            unitsDict[f'{str(a)}.{str(z)}'] = i_1.find_element_by_tag_name('a').get_attribute('href')
            # 将课程链接放入字典中
            print(f'---{str(a)}.{str(z)}{i_1.text}')
            # 输出目录
    unitsId = list(unitsDict.keys())
    pickUnitsId = unitsId[unitsId.index(startId):unitsId.index(endId)+1]
    # 挑出所选课程的键
    pickUnitsObj = [unitsDict[i_2] for i_2 in pickUnitsId]
    return pickUnitsObj

def initStart(driver,url):  #进入课程,让视频播放
    driver.get(url)
    for i in range(2,4):
        try:
            time.sleep(2)
            driver.switch_to.frame('iframe')  #切换视频frame
            iframe = driver.find_element_by_css_selector(".ans-attach-online.ans-insertvideo-online")  #定位播放器位置
            driver.switch_to.frame(iframe)  #切换播放器iframe
            driver.find_element_by_css_selector(".vjs-big-play-button").click()  #点击播放
            #paused = driver.find_element_by_xpath('//*[@id="video"]/div[4]/button[1]') #确定播放按钮
            #ActionChains(driver).move_to_element(paused)
            time.sleep(3)
            driver.find_element_by_xpath('//*[@id="video"]/div[4]/button[1]').click()
            time.sleep(3)
            return driver.find_element_by_xpath('//*[@id="video"]/div[4]/div[4]/span[2]').text
            break
        except Exception as e:
            print(e)
            driver.get(url)
            time.sleep(1)
            driver.find_element_by_id(f'dct{i}').click()
            print("本页非播放播放页,切换")

def test(driver):   #破解测试题,目前只有判断题
    try:
        for i in driver.find_elements_by_name("ans-videoquiz-opt"):  #判断题对错元素
            i.click()
            driver.find_element_by_class_name('ans-videoquiz-submit').click() #提交答案元素
            time.sleep(1)
            try:
                driver.find_element_by_class_name('ans-videoquiz-submit').click()
            except Exception as e:
                if "Alert" == str(e).split(' ')[0]:
                    driver.switch_to.alert.accept()
                else:
                    print(e)
    except:
        pass

def check(driver):
    try:
        if driver.find_element_by_xpath('//*[@id="video"]/div[4]/button[1]/span[2]').text == '播放':
            try:
                driver.find_element_by_xpath('//*[@id="video"]/div[4]/button[1]').click()#用于检测是否在播放,如果无法点击,判断是否遇到题目
                return False
            except:
                test(driver)
        elif driver.find_element_by_xpath('//*[@id="video"]/div[4]/button[1]/span[2]').text == '重播':
            return True
        else:
            return False
    except Exception as e: 
        print(e)
        return False

def main():
    driver=webdriver.Chrome()  
    logon(driver)
    enterCourse(driver,str(input('请输入课程url:\n')))
    time.sleep(1)
    units = acquireUnits(driver,kaishi,jieshu)
    for i in units:
        try:
            allTime = initStart(driver,units[i])
            print(f'当前视频时间:{allTime}')
            allTime = int(allTime[:2]) * 60 + int(allTime[-2:])
            while True:
                if check(driver):
                    print("本视频结束,进入下一个")
                    break
                time.sleep(5)
        except:
            break

if __name__ == "__main__":
    driver=webdriver.Chrome()  
    logon(driver)
    enterCourse(driver,str(input('请输入课程url:\n')))
    time.sleep(1)
    units = acquireUnits(driver,kaishi,jieshu)
    for i in units:
        try:
            allTime = initStart(driver,units[i])
            print(f'当前视频时间:{allTime}')
            allTime = int(allTime[:2]) * 60 + int(allTime[-2:])
            while True:
                if check(driver):
                    print("本视频结束,进入下一个")
                    break
                time.sleep(5)
        except:
            break
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值