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

使用Undetected Chromedriver启动YouTube报[WinError 6]的解决方法

Undetected Chromedriver启动YouTube时出现WinError 6的解决办法

问题场景

运行以下代码启动YouTube:

import undetected_chromedriver as uc

if __name__ == "__main__":
    driver = uc.Chrome(headless=True)
    driver.get("https://youtube.com")

出现如下错误:

Exception ignored in: <function Chrome.__del__ at 0x00000271674DBEC0>
Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 843, in __del__
  File "C:\Users\PC\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 798, in quit   
OSError: [WinError 6] The handle is invalid

可行解决办法

  • 显式使用try-finally确保资源释放
    该错误大多是Python垃圾回收触发__del__方法时,浏览器进程已提前关闭,导致调用quit()失败。用try-finally块显式控制关闭逻辑:

    import undetected_chromedriver as uc
    
    if __name__ == "__main__":
        driver = uc.Chrome(headless=True)
        try:
            driver.get("https://youtube.com")
            # 在此添加需要执行的操作
        finally:
            driver.quit()
    
  • 更新Undetected Chromedriver到最新版本
    该错误可能是旧版本的已知bug,执行以下命令更新:

    pip install --upgrade undetected-chromedriver
    
  • 禁用自动关闭并手动管理进程
    初始化Chrome时设置auto_close=False,避免自动触发可能出错的关闭逻辑,但必须手动调用quit():

    import undetected_chromedriver as uc
    
    if __name__ == "__main__":
        driver = uc.Chrome(headless=True, auto_close=False)
        driver.get("https://youtube.com")
        # 使用完成后手动关闭
        driver.quit()
    
  • 手动指定Chrome可执行文件路径
    如果系统中Chrome路径不标准,可能导致进程管理异常,手动指定路径:

    import undetected_chromedriver as uc
    from selenium.webdriver.chrome.service import Service
    
    if __name__ == "__main__":
        # 替换为你系统中Chrome的实际路径
        chrome_path = "C:/Program Files/Google/Chrome/Application/chrome.exe"
        service = Service(executable_path=chrome_path)
        driver = uc.Chrome(service=service, headless=True)
        try:
            driver.get("https://youtube.com")
        finally:
            driver.quit()
    

内容的提问来源于stack exchange,提问作者Alt Acc Zeprix

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 15:44:51