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

Selenium(Python)打开YouTube无法登录及安全验证问题求助

解决Selenium Chrome登录YouTube时的"浏览器不安全"问题

我之前也踩过这个坑!YouTube的反自动化检测会识别出Selenium驱动的Chrome,所以才会弹出那个“浏览器不安全”的提示。下面几个方法亲测有效,你可以试试看:

方法1:加载本地Chrome用户配置

这个方法最省心,直接复用你日常使用的Chrome用户数据——相当于让Selenium启动你平时登录过YouTube的那个浏览器环境,自然不会触发安全提示。

修改你的代码如下:

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 替换成你自己的Chrome用户数据路径,Windows一般是这个格式,注意用双反斜杠或者原始字符串
chrome_options.add_argument(r"user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data")
# 指定配置文件夹,默认是Default,如果你有多个Chrome用户可以改对应名称
chrome_options.add_argument(r"profile-directory=Default")

# 初始化浏览器时传入配置
browser = webdriver.Chrome(executable_path="C:\\Users\\Pranav Sandeep\\Downloads\\chromedriver.exe", options=chrome_options)
browser.get('https://www.youtube.com')
SearchBar = browser.find_element_by_name("search_query").send_keys("Selenium", Keys.ENTER)
Video = browser.find_element_by_id("video-title")
Video.click()

⚠️ 注意:运行这段代码前要关闭所有正在运行的Chrome窗口,否则会出现进程冲突。

方法2:添加浏览器伪装参数

YouTube会通过navigator.webdriver这类特征识别自动化工具,我们可以通过修改启动参数和执行JS来隐藏这些特征,让Selenium的Chrome看起来更像真实用户的浏览器:

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 禁用自动化扩展和提示
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
# 绕过自动化控制检测
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
# 最大化窗口,模拟真实操作
chrome_options.add_argument("--start-maximized")
# 设置真实的用户代理
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")

browser = webdriver.Chrome(executable_path="C:\\Users\\Pranav Sandeep\\Downloads\\chromedriver.exe", options=chrome_options)
# 执行JS隐藏webdriver属性
browser.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

browser.get('https://www.youtube.com')
SearchBar = browser.find_element_by_name("search_query").send_keys("Selenium", Keys.ENTER)
Video = browser.find_element_by_id("video-title")
Video.click()

修改后再尝试登录,大概率不会再弹出安全提示。

方法3:使用undetected-chromedriver库

如果上面的方法都没效果,可以试试这个专门针对反检测的第三方库——它会自动处理Chrome的自动化特征隐藏,绕过大部分网站的反爬机制:

首先安装库:

pip install undetected-chromedriver

然后替换你的代码:

from selenium.webdriver.common.keys import Keys
import undetected_chromedriver as uc

# 初始化浏览器,库会自动配置参数
browser = uc.Chrome()
browser.get('https://www.youtube.com')
SearchBar = browser.find_element_by_name("search_query").send_keys("Selenium", Keys.ENTER)
Video = browser.find_element_by_id("video-title")
Video.click()

这个方法几乎能解决绝大多数Selenium被检测的问题,登录YouTube基本不会有障碍。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 13:47:28