【发布时间】: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