You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python的Selenium登录YouTube并搜索视频?遇验证问题求解

问题:Selenium登录YouTube遇人机验证,如何实现账号密码登录并搜索?

我用Python写了一段Selenium脚本,想登录YouTube并搜索关键词,代码如下:

from selenium import webdriver
import time
driver = webdriver.Chrome()
url = 'https://www.youtube.com'
driver.get(url)
driver.maximize_window()
time.sleep(3)
button = driver.find_element('xpath', "//a[@aria-label='Sign in']//div[@class='yt-spec-touch-feedback-shape yt-spec-touch-feedback-shape--touch-response-inverse']//div[@class='yt-spec-touch-feedback-shape__fill']")
button.click()
Username = driver.find_element('xpath', "//input[@id='identifierId']")
Username.send_keys('EMAIL')
driver.implicitly_wait(3)
Next = driver.find_element('xpath', "//span[normalize-space()='Next']")
Next.click()
time.sleep(3)
driver.implicitly_wait(3)
Password = driver.find_element('xpath', "//input[@name='Passwd']")
Password.send_keys('PASSWORD')
time.sleep(3)
driver.quit()

运行时碰到了人机验证,查了不少技术帖,大多讲用Cookie结合Gmail的方法,没找到直接用账号密码绕过验证的方案。想问怎么绕过这个验证,正常完成账号密码登录和视频搜索?


解决方案

直接用账号密码绕过YouTube的人机验证难度极高——谷歌的反爬机制会精准识别自动化工具特征。以下是几种可行的落地方案:

1. 复用Chrome已登录会话(最稳定)

直接调用你日常使用的Chrome用户数据目录,复用已保存的登录状态和验证信息,完全跳过登录流程:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options = Options()
# 替换为你的Chrome用户数据路径:
# Windows示例:C:\\Users\\你的用户名\\AppData\\Local\\Google\\Chrome\\User Data
# Mac示例:/Users/你的用户名/Library/Application Support/Google/Chrome/Default
options.add_argument(r"user-data-dir=C:\Users\YourName\AppData\Local\Google\Chrome\User Data")

driver = webdriver.Chrome(options=options)
driver.get("https://www.youtube.com")
time.sleep(2)

# 直接执行搜索
search_box = driver.find_element('xpath', "//input[@id='search']")
search_box.send_keys("Python教程")
search_box.submit()
time.sleep(5)

driver.quit()

注意:运行脚本前必须关闭所有正在运行的Chrome窗口,否则会出现进程冲突。

2. 优化自动化行为,降低机器人特征

通过模拟真实用户操作逻辑,减少被识别的概率:

  • 替换固定time.sleep()为随机等待时长
  • 用WebDriverWait显式等待替代隐式等待,避免无效延迟
  • 添加鼠标移动、页面滚动等人类操作
  • 禁用Chrome的自动化识别提示

优化后的示例代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import random

options = Options()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome(options=options)
driver.get("https://www.youtube.com")
driver.maximize_window()

# 随机等待2-4秒,模拟用户加载页面的时间
time.sleep(random.uniform(2,4))

# 显式等待登录按钮可点击后点击
sign_in_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//a[@aria-label='Sign in']"))
)
sign_in_btn.click()

# 等待邮箱输入框可见并输入
email_input = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "identifierId"))
)
email_input.send_keys("你的邮箱")

# 点击下一步
next_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//span[text()='Next']"))
)
next_btn.click()

# 等待密码框可见并输入
pwd_input = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.NAME, "Passwd"))
)
pwd_input.send_keys("你的密码")

# 完成登录(若触发验证,需手动完成一次,后续可复用会话)
login_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//span[text()='Next']"))
)
login_btn.click()

# 随机等待后执行搜索
time.sleep(random.uniform(3,5))
search_box = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "search"))
)
search_box.send_keys("Python教程")
search_box.submit()

time.sleep(random.uniform(5,8))
driver.quit()

这种方法无法100%避免验证,但能大幅降低触发概率;若仍出现验证,手动完成一次后,后续复用该会话即可跳过验证。

3. 第三方验证码识别服务(不推荐)

部分服务可识别谷歌人机验证,但存在账号被盗风险,且违反YouTube服务条款,不建议使用。

内容的提问来源于stack exchange,提问作者NurseNeko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 01:45:26