无法使用Selenium和Python播放YouTube视频
规格:Chrome:version81.0.4044.113(官方版本)(64位) 我想用Selenium播放HTML视频。视频链接可从以下位置访问: 解决此问题的线程可以在线找到,例如 根据提供的建议,起草了以下准则:无法使用Selenium和Python播放YouTube视频,python,selenium,iframe,html5-video,Python,Selenium,Iframe,Html5 Video,规格:Chrome:version81.0.4044.113(官方版本)(64位) 我想用Selenium播放HTML视频。视频链接可从以下位置访问: 解决此问题的线程可以在线找到,例如 根据提供的建议,起草了以下准则: from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from se
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
browser = webdriver.Chrome(executable_path=r"Browsers\chromedriver.exe",
options=chrome_options)
browser.get("https://www.youtube.com/watch?v=nXbfZL5kttM")
WebDriverWait(browser, 40).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://www.youtube.com/watch?v=nXbfZL5kttM']")))
WebDriverWait(browser, 40).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ytp-large-play-button ytp-button']"))).click()
但是,Selenium在第行抛出一个错误:WebDriverWait(浏览器,40)。直到(EC.frame\u to\u be\u available\u和\u switch\u to \u it
)
找不到元素:
{“方法”:“xpath”,“选择器”:“//iframe[包含(@src,)]”
或根据:
类似地,在第行浏览器中抛出了一个类似的错误
是否有可能YouTube改变了他们的框架(例如,iframe
),导致之前的建议不再有效?我试图按照本文的建议检查所有可用的iframe
,但我对如何找到适合我的情况的正确的iframe
有点不知所措
谢谢你的帮助
编辑1:
根据以下建议:
没有错误,但视频没有播放。您不需要找到iframe
,没有这样的。
因此,您的代码应该如下所示:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
browser.get("https://www.youtube.com/watch?v=nXbfZL5kttM")
WebDriverWait(browser, 15).until(EC.element_to_be_clickable(
(By.XPATH, "//button[@aria-label='Play']"))).click()
我希望它能帮助你!你想播放的视频的链接是什么?您好@0m3r,请参考编辑后的帖子。接下来,你怎么知道没有这样的iframe?如果我想播放来自不同网站的HTML视频,这些知识可能会帮助我。@balandongiv,欢迎您!您可以在Developer T中查看该页面例如,Google Chrome中的ools。
video = browser.findElement(By.id("id video")) # Use the id of the video to find it.
video.click()
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
browser.get("https://www.youtube.com/watch?v=nXbfZL5kttM")
WebDriverWait(browser, 15).until(EC.element_to_be_clickable(
(By.XPATH, "//button[@aria-label='Play']"))).click()