首页 > 解决方案 > 为 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 成绩单的任何帮助。

标签: pythonseleniumyoutubeautomation

解决方案


试试这个代码:

# 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() 

推荐阅读