• [python / selenium]


    前言

    看公选课还是能学到很多知识的,这里是给大家提供一个selenium的使用思路(好好学公选课,我真的看了

    思路

    • 当观看者移动鼠标到某一范围时就会停止播放,就让selenium一直将鼠标悬停在视频处;

    • 视频中途会弹出问题,一旦弹出问题的元素出现,就播放音效提示用户去作答,作答完毕后继续让selenium接管(已实现自动作答,但仍存在小问题)

    • 当每节观看时间任务达成后,自动点击下一视频进行播放。

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.action_chains import ActionChains
    from playsound import playsound
    from selenium.webdriver.support import expected_conditions as EC
    import time
    '''
    
    使用前提:
    使用前需要将googlechromeapplication文件夹加入到环境变量中,同时下载相应的webdriver版本也将其放到application文件夹下面
    然后在在cmd中键入chrome.exe --remote-debugging-port=9222 --user-data-dir="C:selenumAutomationProfile"
    同时需要自行登陆进入视频学习界面(暂时没有研究出不靠selenium接管直接破验证码的方式)
    
    存在的问题:
    基本上视频中的弹框,选择第一个都能解决问题,在之后的测试中遇到答案错误情况极其少,所以弹框点击部分可能存在一定问题
    
    '''
    chrome_options = Options()
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    # chrome_driver地址
    chrome_driver = "C:Program Files (x86)GoogleChromeApplicationchromedriver.exe"
    driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
    time.sleep(2)
    a = driver.find_elements_by_xpath("//div[@class='ncells']//a")
    title = []
    element = []
    tt = []
    # 获取的和标题的每节的名称存在空格的区别
    for i in a:
        title.append(i.get_attribute("title"))
        tt.append(str.strip(i.get_attribute("title")))
    
    
    def do():
        t = driver.find_element_by_tag_name("h1").text
        print("现在正在学习的是:", t)
        driver.switch_to.frame('iframe')
        iframe = driver.find_element_by_tag_name("iframe")
        iframe.click()
        oldtime = -1
        i = 1
        while True:
            ActionChains(driver).move_to_element(iframe).perform()
            driver.switch_to.frame(iframe)
            # 有时候会自动暂停,如果在x秒内,进度没有变,就进行点击行为
            if i == 1:
                time.sleep(3)
                i = 100
            process = float(driver.find_element_by_xpath("//div[@role='slider']").get_attribute("aria-valuenow"))
            print("现在已经播放了:", process)
            if process == oldtime:
                driver.switch_to.parent_frame()
                time.sleep(3)
                iframe.click()
                print("已经恢复播放")
                driver.switch_to.frame(iframe)
                time.sleep(3)
            else:
                oldtime = process
            if process > 90:
                # 当视频播放完毕后,播放音效提示
                # playsound("7371.mp3")
                print("该节已经播放完毕")
                driver.switch_to.parent_frame()
                time.sleep(3)
                driver.switch_to.parent_frame()
                time.sleep(5)
                newt = (tt.index(str.strip(t)))+1
                newt = title[newt]
                driver.find_element_by_xpath("//a[@title='"+newt+"']").click()
                time.sleep(3)
                try:
                    # 对于每一章的第一个视频因为有三个选项,所以首先需要点击第二个视频部分
                    driver.find_element_by_xpath("//span[@title='学习目标']")
                    driver.find_element_by_xpath("//span[@title='视频']").click()
                except:
                    pass
                print("现在来到了新视频:", driver.current_url)
                t = str.strip(driver.find_element_by_tag_name("h1").text)
                print("现在正在学习的是:", t)
                time.sleep(2)
                driver.switch_to.frame('iframe')
                iframe = driver.find_element_by_tag_name("iframe")
                iframe.click()
                oldtime = -1
                i = 1
                continue
            try:
                # 视频中途弹出问题就播放音效提示去作答
                driver.find_element_by_xpath("//input[@name='ans-videoquiz-opt']")
                # playsound("12105.mp3")
                print("----------答题时间到了----------")
                # 选第一个基本上都是对的
                driver.find_elements_by_xpath("//input[@type='radio']")[0].click()
                time.sleep(2)
                driver.find_element_by_xpath("//div[@class='ans-videoquiz-submit']").click()
                try:
                    # 可能还是存在问题,没机会测试了
                    alt = driver.switch_to.alert()
                    alt.accept()
                    driver.switch_to.parent_frame()
                    print("----------点击弹框了!----------")
                    driver.find_elements_by_xpath("//input[@type='radio']")[1].click()
                    time.sleep(2)
                    driver.find_element_by_xpath("//div[@class='ans-videoquiz-submit']").click()
                except:
                    pass
                print('答题已经完成')
            except:
                pass
            time.sleep(15)
            driver.switch_to.parent_frame()
    
    
    do()
    

    效果展示

    在这里插入图片描述

  • 相关阅读:
    登录注册页面切换
    LINUX系统日常使用命令
    find命令详解
    ssh命令详解
    tar命令详解
    route命令详解
    uname命令详解
    ps命令详解
    df命令详解
    virsh命令详解
  • 原文地址:https://www.cnblogs.com/NFii/p/11767964.html
Copyright © 2020-2023  润新知