首页 > 解决方案 > 如何通过 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()

标签: pythonselenium-webdriverxpathcss-selectorswebdriverwait

解决方案


尝试了下面的代码,但给出了错误。

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:

推荐阅读