- 自动观看视频
from builtins import print
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
# 打开网页
browser = webdriver.Chrome()
browser.get('http://xxx/')
# 手动登陆
for i in range(30):
print('\r%d'%(30-i), end='', flush=True)
time.sleep(1)
print()
# 切换到新的窗口
handles = browser.window_handles
browser.switch_to.window(handles[-1])
now = lambda: time.time()
start = now()
reverse = True
while True:
# 视频列表
# video_list = browser.find_elements_by_class_name("f-ib w100 text-etc") # class复合属性不支持这么写
# video_list = browser.find_elements_by_css_selector(".f-ib .w100 .text-etc") # css中间也不能写空格的
video_list = browser.find_elements_by_css_selector(".f-ib.w100.text-etc") # css 支持找类class
# 解决点击第一个a之后,页面出现刷新,再想点第二个就会报错
video_num = len(video_list)
if video_list:
print('找到播放列表')
for i in range(video_num):
video_id = video_num-1-i if reverse else i
video = browser.find_elements_by_css_selector(".f-ib.w100.text-etc")[video_id]
#选择播放列表中的视频
video.click()
print('选择播放')
# 视频播放按钮
play_button = browser.find_element_by_css_selector(".vjs-big-play-button")
# time.sleep(2)
# play_button.click()
is_visible = WebDriverWait(browser, 6).until(lambda driver: play_button.is_displayed())
if is_visible:
play_button.click()
print('开始播放')
# 等待视频结束按钮
end_button = None
# waite_50 = False
start_time = now()
while not end_button :
try:
end_button = browser.find_element_by_css_selector(".btn.btn-primary:not(.btnSubmitQuestion)")
except:
time.sleep(30)
print("视频没有结束,等待30s")
# 可能视频看完不会跳出弹框,50分钟后直接下一个视频
if int(now() - start_time) > 60*50:
break
else:
continue
else:
print("视频结束,退出循环")
time.sleep(10)
# 点击结束弹框
end_button.click()
#遍历完播放列表就退出
break
else:
if int(now() - start) < 60:
time.sleep(10)
# 一分钟没有找到视频列表,直接退出
break