【问题标题】:Automate to subscribe a YouTube video自动订阅 YouTube 视频
【发布时间】:2021-03-09 02:10:51
【问题描述】:

我是一名学习 Selenium webdriver 的 Python 程序员。通过使用 XPATH,我能够像在任何 YouTube 视频上一样进行表演,但我无法弄清楚如何通过 selenium 订阅同一视频所有者的频道。 查看下面的代码,请提出一些纠正此问题的方法,如果正确,请建议find_element_by_ 检测/查找订阅按钮的当前方法。

import time
from selenium import webdriver
from chromedriver_py import binary_path

driver = webdriver.Chrome(executable_path=binary_path)
driver.get('https://www.youtube.com/watch?v=7G72oJyfhKk')

time.sleep(2)

#for like button (works fine):
like_btn = driver.find_element_by_xpath('//div[@id="info"]//ytd-toggle-button-renderer[1]//a[1]//yt-icon-button[1]//button[1]//yt-icon[1]')
like_btn.click()

#for subscribe button (didn't work):
driver.execute_script("window.scrollTo(0, 300)") #scrolling down 300px to make subscribe button visible
subs_btn = driver.find_element_by_class_xpath('//*[@id="button"]')
subs_btn.click()

在运行上面的代码时,它可以正常工作到喜欢按钮,然后它会引发以下错误:

selenium.common.exceptions.ElementNotInteractableException: 消息:元素不可交互

【问题讨论】:

    标签: python selenium youtube webdriver


    【解决方案1】:

    对于订阅按钮,请尝试将以下代码与相对 xpath 一起使用

    subscribe_btn = driver.find_element_by_xpath('//yt-formatted-string[contains(text(),'Subscribe')]')
    subscribe_btn.click()
    

    请注意,此解决方案仅适用于英文版本。

    【讨论】:

    • 抱歉,这不起作用。它仍然会引发
    • 你能在 chrome 页面上再次检查一下吗:ibb.co/QNdbhdd
    • 谢谢,但我知道你通过检查 YouTube 网页给了我订阅按钮的 xpath,即使我做了同样的事情,但 YouTube 有一些特殊的编码,它不会让我们知道正确的 xpath 或其他相关内容到那个元素。我建议你自己试试吧
    猜你喜欢
    • 2015-08-16
    • 2013-03-16
    • 2015-10-14
    • 2021-02-19
    • 1970-01-01
    • 2011-10-20
    • 2012-12-02
    • 1970-01-01
    相关资源
    最近更新 更多