我想写一个使用Selenium的python脚本,当一个嵌入的YouTube视频结束时点击一个按钮。
如果无法检测到视频何时结束,我可以在3-5秒内没有声音的情况下单击按钮。
发布于 2021-05-31 22:03:25
要确定视频是否已完成,请尝试检查"ytp-time-current“类是否等于"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')https://stackoverflow.com/questions/67775115
复制相似问题