使用user-data-dir调用Edge时遇DevToolsActivePort/Chrome不可达错误
问题描述
尝试自动化收集Cookie,使用Microsoft Edge内置浏览器,要求使用已登录的用户配置,尽可能以无头模式运行。但在配置user-data-dir和profile-directory参数时出现报错:
- 初始错误:
DevToolsActivePort not found - 添加
remote-debugging-port=8000参数后,错误变为chrome not reachable
无头模式运行时无报错,但无法加载已登录的Microsoft/Google账号状态。使用Edge或ChromiumEdge类无区别。
现有Python代码(基于Selenium)
import time import os from selenium.webdriver import EdgeService from selenium.webdriver import ChromiumEdge from selenium.webdriver import EdgeOptions # service = EdgeService(r'assets\msedgedriver.exe') # service.start() options = EdgeOptions() options.add_argument(f"user-data-dir=C:\\Users\\{os.getlogin()}\\AppData\\Local\\Microsoft\\Edge\\User Data") options.add_argument("profile-directory=Default") options.add_argument("--disable-dev-shm-using") # an answer on stackoverflow which did not work options.add_argument("remote-debugging-port=8000") # another one which did not work options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe" driver = ChromiumEdge(options=options) driver.get("https://google.com") # driver.get("https://bing.com") driver.save_screenshot("ss2.png") with open("cookies.txt", "w") as f: contents = "" cookies = driver.get_cookies() for cookie in cookies: contents += cookie.get('name') + ": " + cookie.get('value') + "\n" f.write(contents)
报错信息(管理员权限运行PyCharm控制台输出)
Traceback (most recent call last): File "C:\path\to\project\test2.py", line 18, in <module> driver = ChromiumEdge(options=options) File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\edge\webdriver.py", line 45, in __init__ super().__init__( File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 56, in __init__ super().__init__( File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 208, in __init__ self.start_session(capabilities) File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 292, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute self.error_handler.check_response(response) File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Microsoft Edge failed to start: exited normally. (chrome not reachable) (The process started from msedge location C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe is no longer running, so msedgedriver is assuming that msedge has crashed.) Stacktrace: GetHandleVerifier [0x00007FF6149E2602+60402] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF614967352+253666] (No symbol) [0x00007FF614739C99] (No symbol) [0x00007FF614772FB2] (No symbol) [0x00007FF61476D18B] (No symbol) [0x00007FF6147B423F] (No symbol) [0x00007FF6147AB863] (No symbol) [0x00007FF61477C585] (No symbol) [0x00007FF61477B993] (No symbol) [0x00007FF61477CD14] Microsoft::Applications::Events::EventProperty::to_string [0x00007FF614BC34A4+1161924] (No symbol) [0x00007FF6147FE5F6] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6148BB083+37459] Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6148B2C4D+3613] Microsoft::Applications::Events::EventProperty::to_string [0x00007FF614BC21E4+1157124] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6149716A8+295480] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF61496CB74+276228] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF61496CCA2+276530] Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF61495FA91+222753] BaseThreadInitThunk [0x00007FFF6B647344+20] RtlUserThreadStart [0x00007FFF6B7826B1+33] Process finished with exit code 1
解决方案
关键修复点
- 关闭所有Edge进程:如果当前有Edge浏览器正在运行,会占用用户数据目录,导致Selenium无法启动新实例。打开任务管理器,结束所有
msedge.exe进程。 - 修正参数拼写错误:
--disable-dev-shm-using应为--disable-dev-shm-usage,原拼写无效。 - 使用新版无头模式:旧的
--headless参数不会加载用户配置,需改用--headless=new,同时添加必要的无头模式配套参数。 - 匹配驱动版本:确保
msedgedriver.exe的版本与本地安装的Microsoft Edge版本完全一致(可在edge://settings/help查看Edge版本,下载对应驱动)。 - 移除不必要的参数:
remote-debugging-port在这种场景下无需添加,反而可能引发冲突。
修改后的代码
import os from selenium.webdriver import ChromiumEdge from selenium.webdriver import EdgeOptions options = EdgeOptions() # 指定用户数据目录,使用已登录的配置文件 user_data_dir = f"C:\\Users\\{os.getlogin()}\\AppData\\Local\\Microsoft\\Edge\\User Data" options.add_argument(f"user-data-dir={user_data_dir}") options.add_argument("profile-directory=Default") # 新版无头模式参数,支持加载用户配置 options.add_argument("--headless=new") options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--remote-allow-origins=*") # 指定Edge二进制文件路径 options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe" # 初始化驱动 driver = ChromiumEdge(options=options) try: driver.get("https://google.com") # 保存截图验证状态 driver.save_screenshot("ss2.png") # 收集并保存Cookie with open("cookies.txt", "w", encoding="utf-8") as f: for cookie in driver.get_cookies(): f.write(f"{cookie['name']}: {cookie['value']}\n") finally: # 确保驱动关闭 driver.quit()
内容的提问来源于stack exchange,提问作者Melting Obsidian
相关产品推荐
相关产品推荐

