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

Python Selenium启动Chrome报Devtools端口格式异常如何解决?

问题根因与解决方案

错误原因

你的报错是Chrome的DevToolsActivePort端口文件损坏/读取异常,核心触发原因是配置文件参数写法错误,同时还有旧版无头模式兼容问题、后台残留进程的影响。

解决步骤

  • 修正配置文件加载参数:user-data-dir仅需指定到Chrome用户数据根目录User Data,单独用profile-directory参数指定具体配置文件名,不要把配置文件完整路径写在user-data-dir中
  • 清理后台残留进程:关闭所有Chrome浏览器窗口,执行命令清理残留进程:
    taskkill /f /im chrome.exe
    taskkill /f /im chromedriver.exe
    
  • 替换无头模式参数:新版Chrome旧版--headless参数存在兼容问题,替换为--headless=new
  • 可选操作:如果仍报错,进入C:\Users\Owner\AppData\Local\Google\Chrome\User Data\Profile 8目录,删除DevToolsActivePort文件后重试

修正后可运行代码

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

chrome_driver_path = "C:\\Development\\chromedriver.exe"
options = Options()
options.binary_location = "C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"
# 修正配置文件参数写法
options.add_argument("user-data-dir=C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("profile-directory=Profile 8")
# 替换为新版无头模式
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.add_argument("--start-maximized")
# 可选:固定调试端口避免端口文件生成异常
options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(executable_path=chrome_driver_path, options=options)

driver.get(url="https://www.google.com")
print(driver.title)
driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 05:36:01