python - 如何通过 Python 使用 Selenium 评论实时 youtube 视频
问题描述
我正在尝试评论直播 youtube 视频。
但我收到错误
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id='label']"}
这是我的代码。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_path = r"xxxx"
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_path,chrome_options=chrome_options)
driver.get('https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dcomment%26app%3Ddesktop%26next%3D%252Fall_comments%253Fv%253D-N4ztwGnw90%26action_handle_signin%3Dtrue&uilel=3&service=youtube&passive=true&hl=en')
driver.implicitly_wait(10)
# post a comment
comment = "test"
driver.find_element_by_xpath("//input[@id='label']").send_key('Hello')
driver.find_element_by_xpath("//button[@class='style-scope yt-button-renderer' and @id='button']").click()
解决方案
尝试了下面的代码,但给出了错误。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import unittest
from selenium.common.exceptions import TimeoutException
chrome_path = r"xxxx"
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_path,chrome_options=chrome_options)
driver.get('https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dcomment%26app%3Ddesktop%26next%3D%252Fall_comments%253Fv%253D6-S4et2YpZc%26action_handle_signin%3Dtrue&uilel=3&service=youtube&passive=true&hl=en')
# post a comment
comment = "test"
boxFind = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//label[@class='style-scope yt-live-chat-text-input-field-renderer']"))).send_keys("Novask")
错误
File "C:\Python3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
推荐阅读
- sql - 在 SQL 中将 WHERE 和 CASE 与多个条件相结合
- javascript - setTimeout 搜索
- python - 使用matplotlib在散点图中连接k个最近邻居的有效方法
- java - Spring Security 没有做好它的工作
- php - Angularjs stateProvider 不适用于 xampp。它仅适用于 nodejs
- anylogic - 如何在 Anylogic 上使用 Java 方法访问每个点节点?
- json - 在swift3中重新加载表数据
- c# - 如何将标志属性与从枚举继承的类一起使用
- speech-recognition - 如何在 kaldi 中使用“tri2a”和“tri2b_mmi”培训傻瓜教程?
- c# - 无法更新 Abp.Zero.Ldap NuGet 包