python - 为 Youtube 自动生成的字幕自动打开脚本
问题描述
我正在尝试编写一个 python 脚本,该脚本将自动单击打开 youtube 视频的脚本。
例如,如果您访问 youtube 视频,在分享按钮右侧的两个(由 3 个点表示),您可以打开 youtube 视频的脚本。
到目前为止,我的代码打开了 URL,并打开了隐藏式字幕(有效)。
from selenium import webdriver
import requests
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=q1RYI034sH0")
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "ytp-subtitles-button")))
element.click()
但是,我知道想通过添加以下代码来打开脚本,这会产生错误,因为它找不到按钮。
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "yt-icon-button)")))
我不太确定从这里开始做什么,非常感谢自动打开 youtube 成绩单的任何帮助。
解决方案
试试这个代码:
# opens 'More actions' menu
more_action_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label = 'More actions']")))
more_action_btn.click()
# clicks on 'open transcription' button
open_trancript_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//paper-listbox[@id = 'items']/ytd-menu-service-item-renderer")))
open_trancript_btn.click()
推荐阅读
- java - 编译为 jar 时文档不起作用
- flutter - 垂直居中文本并与图像右对齐
- visual-studio - NUnit3.TestAdapter 安装问题
- react-native - Android 7 React Native 上的启动画面上的应用程序崩溃
- javascript - 谷歌表格使用脚本将一列翻译成另一列
- github - 向 GitHub 发送电子邮件无回复电子邮件 ID
- java - Java WeakReference 测试未按预期工作
- javascript - 传递最后一项时,如何将最后 N 个值存储在具有自动循环到第一项的数组中?
- c++ - C++ 语言中的动态内存分配
- mysql - 查询包含对象的json数组