selenium学习之处理HTML5视频播放
HTML5中定义了新元素,该元素提供了javascript接口和多种的方法和属性,javascript函数有个内置的对象
arguments。arguments对象包含了函数调用的参数数组,[0]表示去对象的第一个值,currentSrc返回当前视频/音频的URL,如果未设置的时候,返回NULL。
并且提供了load(),play(),pause()方法来加载,播放,暂停视频
PS:使用find_element_by_xpath()来定位元素的时候,注意需要一层一层逐层定位,并且有些元素登陆前后的id是有变化的,有些这个根据文档中提供的代码,查了好久的资料才搞定的,注意不要踩坑~~~
# coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("http://videojs.com")
video = driver.find_element_by_xpath('//*[@id="preview-player_html5_api"]')
url = driver.execute_script("return arguments[0].currentSrc;", video)
print(url)
print("开始")
driver.execute_script("return arguments[0].play()", video)
time.sleep(15)
'播放15S
print("结束")
driver.execute_script("return arguments[0].pause()", video)
driver.quit()
'''
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)
#截图
driver.get_screenshot_as_file('d:/baidu.png')
#暂停播放
print("stop")
driver.execute_script("return arguments[0].pause()",video)
#暂停5s
sleep(5)
except BaseException as msg:
print(msg)
finally:
driver.quit()
'''