如何通过 selenium 和 python 单击嵌入在 smtebook 中的 youtube 视频的播放按钮 [英] How to click on the play button of a youtube video embedded within smtebook through selenium and python
本文介绍了如何通过 selenium 和 python 单击嵌入在 smtebook 中的 youtube 视频的播放按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想点击 https://smtebooks.us/downfile/13192/building-serverless-python-web-services-zappa-pdf
我的代码是:
browser.switch_to.frame(0)
element = browser.find_element_by_xpath("//button[@class='ytp-large-play-button ytp-button']")
element.click()
但找不到元素
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='ytp-large-play-button ytp-button']"}
(Session info: chrome=67.0.3396.99)
(Driver info: chromedriver=2.37.543627 (63642262d9fb93fb4ab52398be4286d844092a5e),platform=Windows NT 10.0.17134 x86_64)
有人知道如何处理吗?
谢谢!
推荐答案
为了能够处理嵌入式视频播放器,您需要切换到适当的 iframe 并等待按钮出现在 DOM 中:
To be able to handle embedded video player you need to switch to appropriate iframe and wait for button to appeared in DOM:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser.switch_to.frame(browser.find_element_by_xpath('//iframe[starts-with(@src, "https://www.youtube.com/embed")]'))
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[@aria-label="Play"]'))).click()
这篇关于如何通过 selenium 和 python 单击嵌入在 smtebook 中的 youtube 视频的播放按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文