首页 > 解决方案 > 当我在 YouTube 上添加评论时,它不会出现 python selenium

问题描述

我使用以下代码在YouTube上添加评论但是当我添加评论时,刷新页面后它没有出现。但是当我在我的个人浏览器中发表评论时,评论正常显示我认为谷歌知道我使用的是非个人浏览器所以它不显示评论是否有解决问题的想法?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium_stealth import stealth
import time
import os
import pickle
urlVideo = str(input('Enter your URL video: '))

import io
with io.open('message.txt', "r", encoding="utf-8") as f:
    message = f.readlines()

options = webdriver.ChromeOptions()
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=options)
stealth(driver,
    languages=["en-US", "en"],
    vendor="Google Inc.",
    platform="Win32",
    webgl_vendor="Intel Inc.",
    renderer="Intel Iris OpenGL Engine",
    fix_hairline=True,
    )

driver.get("https://www.youtube.com/")

#input('login and click start')
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

driver.get(urlVideo)
#click comments button
while True:
    try:
        driver.find_element_by_xpath('/html/body').send_keys(Keys.PAGE_DOWN)
        time.sleep(0.5)
        driver.find_element_by_xpath('//*[@id="placeholder-area"]').click()
        driver.find_element_by_xpath('//*[@id="contenteditable-root"]').send_keys(message)
        break
    except:
        pass

time.sleep(2)
while True:
    try:
        driver.find_element_by_css_selector('.style-scope.ytd-button-renderer.style-primary.size-default').click()
        break
    except:
        pass

input('ok')

标签: pythonpython-3.xseleniumselenium-webdriver

解决方案


Youtube 检测使用多个机器人标志,例如代理分数、硒检测、用户代理、过去的帐户活动等。你需要绕过所有这些来欺骗 youtube!我为客户制作了一个 youtube 机器人,为此我做了很多测试,这就是我知道这一点的原因。你需要让你的 chrome 驱动程序无法被检测到,并且表现得像人而不是机器人。


推荐阅读