zoukankan      html  css  js  c++  java
  • selenium处理HTML5视频播放未能自动播放解决办法

    网上找的大部分关于selenium视频播放部分都是老脚本了,不能执行成功!!!

    原因是谷歌18年6月起对autoplay功能做了限制。

    直接上修改后的可运行代码

    from time import sleep
    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("https://videojs.com/")
    video = driver.find_element_by_id("preview-player_html5_api")

    # 仅限于chrome浏览器,原因是谷歌限制自动播放必须有交互行为:例如点击事件 must_click
    =driver.find_element_by_xpath("//*[@id='___gatsby']/div/div/div/a/h1") must_click.click() # 返回播放文件地址,(currentSrc返回当前音频/视频URL,若无返回空字符串) url = driver.execute_script("return arguments[0].currentSrc;", video) print(url) # 滚动到视频播放位置 driver.execute_script("return arguments[0].scrollIntoView();", video) # 播放视频 print("start") driver.execute_script("arguments[0].play()", video) # 播放15s sleep(15) # 暂停视频 print("stop") driver.execute_script("arguments[0].pause()", video) driver.quit()

    ps:html5关于新增标签video的简单阐述:

    HTML5中定义了新元素<video>,该元素提供了javascript接口和多种的方法和属性,javascript函数有个内置的对象

    arguments。arguments对象包含了函数调用的参数数组,[0]表示去对象的第一个值,currentSrc返回当前视频/音频的URL,如果未设置的时候,返回NULL。

    并且提供了load(),play(),pause()方法来加载,播放,暂停视频

  • 相关阅读:
    I
    poj 3414 pots (bfs+路径记录)
    hdoj 1495 非常可乐(bfs)
    hdoj 1241 Oil Deposits (dfs)
    hdoj 2612 find a way (两次bfs)
    poj 3984 迷宫问题
    poj 3087 Shuffle'm Up (bfs)
    poj 3126 Prime Path (bfs)
    poj 3279 Fliptile
    hdu_3068 最长回文(Manacher算法)
  • 原文地址:https://www.cnblogs.com/debuging77/p/13095621.html
Copyright © 2011-2022 走看看