Python 视频或声音结束时做点什么
Python do something when Video or sound ends
我想使用 Selenium
编写一个 python
脚本,当嵌入的 YouTube 视频结束时点击一个按钮。
如果无法检测视频何时结束,我可以在 3-5 秒没有声音时单击按钮。
要确定视频是否播放完毕,请尝试检查 class“ytp-time-current”是否等于 class“ytp-time-duration”
例如:
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')
或:
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')
我想使用 Selenium
编写一个 python
脚本,当嵌入的 YouTube 视频结束时点击一个按钮。
如果无法检测视频何时结束,我可以在 3-5 秒没有声音时单击按钮。
要确定视频是否播放完毕,请尝试检查 class“ytp-time-current”是否等于 class“ytp-time-duration”
例如:
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')
或:
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')