python-3.x - 如何通过selenium和python点击嵌入在smtebook中的youtube视频的播放按钮
问题描述
我想点击https://smtebooks.us/downfile/13192/building-serverless-python-web-services-zappa-pdf中的 youtube 播放
我的代码是:
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 中:
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()
推荐阅读
- php - 在 Laravel 5 应用程序中将 Redis 配置为缓存接口
- php - Wordpress API - 发布元字段
- javascript - 我怎样才能跟踪一个的下载进度?
- sql - SQL Server 即时将斜杠添加到日期
- r - 将日期的缩写与月份编号分开
- rest - 查找 curl 或任何其他命令以获取属于 nexus 中特定版本的工件的版本号
- javascript - 使用 lodash 获取嵌套值
- graphql - 以正则表达式为变量的 Gatsby graphql
- amazon-web-services - AWS S3 CLI ls 将不带前缀的目录列出到数组中(和 where 子句)
- spring - 如何使用 JPA 将 3 个表连接到一个表中?