首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python在视频或声音结束时执行某些操作

Python在视频或声音结束时执行某些操作
EN

Stack Overflow用户
提问于 2021-05-31 21:54:04
回答 1查看 69关注 0票数 0

我想写一个使用Seleniumpython脚本,当一个嵌入的YouTube视频结束时点击一个按钮。

如果无法检测到视频何时结束,我可以在3-5秒内没有声音的情况下单击按钮。

EN

回答 1

Stack Overflow用户

发布于 2021-05-31 22:03:25

要确定视频是否已完成,请尝试检查"ytp-time-current“类是否等于"ytp-time-duration”类

例如:

代码语言:javascript
复制
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome('C:\chromedriver\chromedriver.exe')

# Just a random Video
driver.get('https://www.youtube.com/watch?v=nvMUYdr5_g8')

# accepts the cookies
agree_button = driver.find_element_by_xpath('//*[@id="yDmH0d"]/c-wiz/div/div/div/div[2]/div[1]/div[4]/form/div[1]/div/button/span')
agree_button.click()
sleep(7)

# skips the ad after 7 seconds
try:
    skip_button = driver.find_element_by_xpath('//*[@id="skip-button:6"]/span/button')
    skip_button.click()
except:
    print('no ad')
sleep(2)

# defines the position of current time and duration time
current = driver.find_element_by_xpath('//*[@id="movie_player"]/div[28]/div[2]/div[1]/div[1]/span[1]')
duration = driver.find_element_by_xpath('//*[@id="movie_player"]/div[28]/div[2]/div[1]/div[1]/span[3]')

# checks if the current time equals the duration
if current.text == duration.text:
    print('true')
else:
    print('flase')

或者:

代码语言:javascript
复制
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome('C:\chromedriver\chromedriver.exe')

# Just a random Video
driver.get('https://www.youtube.com/watch?v=nvMUYdr5_g8')

# accepts the cookies
agree_button = driver.find_element_by_xpath('//*[@id="yDmH0d"]/c-wiz/div/div/div/div[2]/div[1]/div[4]/form/div[1]/div/button/span')
agree_button.click()
sleep(7)

# skips the ad after 7 seconds
try:
    skip_button = driver.find_element_by_xpath('//*[@id="skip-button:6"]/span/button')
    skip_button.click()
except:
    print('no ad')
sleep(2)

# defines the position of current time and duration time
current = driver.find_element_by_class_name('ytp-time-current')
duration = driver.find_element_by_class_name('ytp-time-duration')

# checks if the current time equals the duration
if current.text == duration.text:
    print('true')
else:
    print('flase')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67775115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档