selenium学习------处理HTML5视频播放

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

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

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

PS:使用find_element_by_xpath()来定位元素的时候,注意需要一层一层逐层定位,这个根据文档中提供的代码,查了好久的资料才搞定的,注意不要踩坑~~~

# coding="utf-8"

from selenium import webdriver
from time import sleep

try:
    driver = webdriver.Firefox()
    driver.get("http://videojs.com/")
    
    #通过xpath来定位元素的时候需要一层一层逐层定位
    video = driver.find_element_by_xpath("/html/body/section[1]/div/video")

    #返回播放文件地址
    url = driver.execute_script("return arguments[0].currentSrc;", video)
    print(url)

    #播放视频
    print("start")
    driver.execute_script("return arguments[0].play()",video)

    #播放15s
    sleep(15)

    #暂停播放
    print("stop")
    driver.execute_script("return arguments[0].pause()",video)

    #暂停5s
    sleep(5)
except BaseException as msg:
    print(msg)
finally:
    driver.quit()

猜你喜欢

转载自blog.csdn.net/u012605082/article/details/80574530
今日推荐