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

指定Chrome默认Profile后Selenium启动报错求助

解决Selenium使用Chrome默认Profile时的DevToolsActivePort报错问题

核心原因

报错提示DevToolsActivePort file doesn't exist通常源于以下几点:

  • Chrome默认Profile被正在运行的Chrome进程占用
  • 未指定具体的Profile子目录
  • 缺少必要的启动参数导致Chrome启动异常

具体解决方案

1. 关闭所有Chrome进程

打开任务管理器,结束所有chrome.exe进程(包括后台运行的实例),确保默认Profile没有被其他Chrome实例占用。

2. 指定具体的Profile子目录

在原有user-data-dir参数基础上,添加--profile-directory明确指定默认Profile的子目录:

chrome_options = webdriver.ChromeOptions()
# 指定Chrome用户数据根目录
chrome_options.add_argument(r"user-data-dir=C:\Users\root\AppData\Local\Google\Chrome\User Data")
# 指定默认Profile目录
chrome_options.add_argument("--profile-directory=Default")

3. 添加必要的启动参数

补充参数避免启动干扰并指定调试端口,解决DevToolsActivePort问题:

# 指定远程调试端口
chrome_options.add_argument("--remote-debugging-port=9222")
# 跳过首次启动引导和默认浏览器检查
chrome_options.add_argument("--no-first-run")
chrome_options.add_argument("--no-default-browser-check")

4. 修改后的完整代码

import random
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

# 待访问网站列表
websites = ["https://www.example.com/", "http://birds.example.com/?bird=airplane&behavior=birth", "https://www.example.com/", "https://www.example.com/", "http://www.example.net/box"]

# 配置Chrome选项,加载默认Profile
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(r"user-data-dir=C:\Users\root\AppData\Local\Google\Chrome\User Data")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_argument("--no-first-run")
chrome_options.add_argument("--no-default-browser-check")

browsers = {
    "Chrome": webdriver.Chrome(options=chrome_options),
    #"Firefox": webdriver.Firefox(),
    #"Brave": webdriver.Chrome(executable_path="/path/to/brave/driver"),
    # "Edge": webdriver.Edge(executable_path="/path/to/edge/driver")
}

# 随机访问次数
num_visits = 5

# 模拟网页访问的函数
def simulate_web_traffic(browser_name):
    browser = browsers[browser_name]
    browser.get(random.choice(websites))
    
    try:
        cookie_button = browser.find_element(By.XPATH, "//span[contains(text(),'Accept cookies;')]")
        cookie_button.click()
    except Exception as e:
        pass

    time.sleep(random.uniform(3, 8))  # 模拟用户停留时间

# 为每个浏览器模拟流量
for browser_name in browsers.keys():
    print(f"Simulating web traffic for {browser_name}:")
    for _ in range(num_visits):
        simulate_web_traffic(browser_name)
    browsers[browser_name].quit()

5. 验证ChromeDriver与Chrome版本匹配

确保使用的ChromeDriver版本和本地安装的Chrome浏览器版本完全一致(大版本必须相同),版本不匹配会直接导致启动失败。

内容的提问来源于stack exchange,提问作者Backyard Barbeque Boys

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 07:06:31