Selenium youtube评论机器人,无法找到youtube评论框

时间:2018-10-28 14:51:21

标签: python selenium

我正式需要帮助。

以下是使用硒.find_element方法进行管登录的完整代码

导航到特定视频,向下滚动并尝试选择一个YouTube评论框。我已经尝试了所有硒方法来找到一个注释框,然后单击它(我不会列出所有注释框),但是没有成功。

有人可以让我知道可能出什么问题了吗?考虑到我已经使用硒方法成功登录到了You Tube,我看不出为什么在查找Youtube注释框时同样的原因不起作用的原因。

import time
import numpy as np
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
from selenium.common.exceptions import NoSuchElementException


def youtube_login():

    email = 'email@gmail.com'
    password = 'password'

    comment = 'We are looking for good people/traders like yourself to join a new trading chatroom. Chatroom has a multi asset live squawk news, economic events notifications and soon live trade callouts and trade stream. For now the main focus is bond futures but all asset classes are welcome. If you are interested please join while its still free https://discord.gg/ssDvDnx?'

        # Browser
    driver = webdriver.Firefox()
    driver.get('https://accounts.google.com/ServiceLogin?hl=en&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue%26next%3D%252F&uilel=3&passive=true&service=youtube#identifier')
    #driver.maximize_window()

        # log in
    driver.find_element_by_id('identifierId').send_keys(email)
    driver.find_element_by_class_name('CwaK9').click()
    WebDriverWait(driver, 500).until(EC.element_to_be_clickable((By.NAME, "password")))
    driver.find_element_by_name('password').send_keys(password)
    WebDriverWait(driver, 500).until(EC.element_to_be_clickable((By.CLASS_NAME, "CwaK9")))
    driver.find_element_by_class_name('CwaK9').click()
    WebDriverWait(driver, 500).until(EC.element_to_be_clickable((By.ID, "identity-prompt-confirm-button")))
    driver.find_element_by_id('identity-prompt-confirm-button').click()


    urls = []

    # You can add in a file and import from there

    inp = open ("urls.txt","r")
    for line in inp.readlines():
        urls.append(line.strip())
        inp.close()

    for url in urls:  
        driver.get(url)
        time.sleep(5)
        # Scroll, wait for load comment box
        driver.execute_script("window.scrollTo(0, 500);")

        # Lets wait for comment box
        box = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.ID, "emoji")))

        # Activate box for comments
        box.click()

        driver.implicitly_wait(5)
        driver.find_element_by_id("emoji").send_keys(comment)



youtube_login()

3 个答案:

答案 0 :(得分:1)

要触发评论框,请单击ID为 placeholder-area 的元素,然后将显示表情符号输入。

之后,使用ID为' contenteditable-textarea '的输入来sendKeys('comment text'),如果这样做不起作用,则从驱动程序发送可执行脚本:

  

document.getElementById('contenteditable-textarea')。innerHTML ='您的   在此处评论文字”

答案 1 :(得分:0)

添加此行

 #Activate the box
 commentBox = driver.find_element_by_id('placeholder-area')
 commentBox.click()
 time.sleep(4)

 #Send the keys to the input field
 inputBox =driver.find_element_by_id('contenteditable-root')
 inputBox.send_keys('test')

它在2020年对我有用:)

答案 2 :(得分:0)

这可能有点晚了,但是为什么不看这样的随机视频呢?

while True:
    try:
        recently = chrome_browser.find_element_by_xpath('//yt-formatted-string[@title="Recently uploaded"]')
        recently.click()
        break
    except:
        continue
time.sleep(10)
video = chrome_browser.find_element_by_xpath('//div[@class="style-scope ytd-rich-item-renderer"]')
video.click()
相关问题