Python我无法在YouTube上发表评论

时间:2019-06-06 16:48:46

标签: python selenium selenium-webdriver

我正在尝试在youtube视频上发表评论。 运行脚本时出现错误 请帮我解决错误。

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


chrome_path = r"xxxxxxxx"
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%253DKdsJtbecX_A%26action_handle_signin%3Dtrue&uilel=3&service=youtube&passive=true&hl=en')

# post a comment
comment = "test"

box = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "box")))
box.click()

frame = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//iframe[@title="+1"]')))
driver.switch_to.frame(frame)

driver.find_element_by_xpath('//div[@onclick]').click()

element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//div[@jsname="msEQQc"]/following-sibling::div//div[@g_editable="true"]')))
driver.execute_script("arguments[0].innerHTML='%s';" % comment, element)

这是错误

  File "yt.py", line 17, in <module>
    box = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "box")))
  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:

1 个答案:

答案 0 :(得分:0)

在此步骤

  

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%253DKdsJtbecX_A%26action_handle_signin%3Dtrue&uilel=3&service=youtube&passive=true&hl=en')

您正在导航到youtube的登录页面。

如果您未完成登录过程,则找不到元素

请注意,硒打开浏览器时会保持干净(没有库克语,缓存...)