Python Selenium无法连接Edge调试浏览器问题求助
问题描述
我尝试用Python脚本连接通过命令提示符以调试模式启动的Edge浏览器,但无法连接到127.0.0.1:9222,尝试多种方案仍未解决。
启动Edge的调试命令
"c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222
我的Python脚本
from selenium import webdriver from selenium.webdriver.edge.options import Options edge_options = Options() edge_options.add_experimental_option('debuggerAddress', '127.0.0.1:9222') driver = webdriver.Edge(options=edge_options) driver.get(youtube.com) printf(driver.title)
错误信息
核心错误片段:
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to microsoft edge at 127.0.0.1:9222 **from chrome not reachable **
完整错误栈:
Traceback (most recent call last): File "C:\Users\user\OneDrive\Plocha\záložky\msedge - running\Try 18.py", line 7, in <module> driver = webdriver.Edge(options=edge_options) File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\edge\webdriver.py", line 73, in __init__ super().__init__( File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chromium\webdriver.py", line 104, in __init__ super().__init__( File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 286, in __init__ self.start_session(capabilities, browser_profile) File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 378, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute self.error_handler.check_response(response) File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to microsoft edge at 127.0.0.1:9222 from chrome not reachable Stacktrace: Backtrace: Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72EE616C2+15186] Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EDFA212+827554] (No symbol) [0x00007FF72EABEBF3] (No symbol) [0x00007FF72EAAEC8A] (No symbol) [0x00007FF72EAF013D] (No symbol) [0x00007FF72EAE5D4A] (No symbol) [0x00007FF72EB29C56] (No symbol) [0x00007FF72EB21D23] (No symbol) [0x00007FF72EAF3B80] (No symbol) [0x00007FF72EAF2B0E] (No symbol) [0x00007FF72EAF4344] Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72ECDC3B0+182752] (No symbol) [0x00007FF72EBB0095] Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72ED3A6EA+42362] Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72ED3D425+53941] Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF72F058AB3+1456595] Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE0276A+861690] Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE07854+882404] Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE079AC+882748] Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE1097E+919566] BaseThreadInitThunk [0x00007FF8D3C726BD+29] RtlUserThreadStart [0x00007FF8D5A6DFB8+40]
我已经尝试过多个教程和资源,但均未解决问题。
解决方案
1. 彻底关闭Edge后再启动调试模式
打开任务管理器,结束所有msedge.exe进程,再重新运行调试启动命令。残留的Edge进程会导致端口冲突或会话异常。
2. 严格匹配Edge与EdgeDriver版本
- 打开Edge,进入「设置→关于Microsoft Edge」查看浏览器版本号
- 下载对应版本的EdgeDriver,将其放置在Python环境的PATH目录下,或在代码中指定路径:
from selenium.webdriver.edge.service import Service from selenium import webdriver from selenium.webdriver.edge.options import Options edge_options = Options() edge_options.add_experimental_option('debuggerAddress', '127.0.0.1:9222') # 指定EdgeDriver路径 service = Service(r'C:\path\to\msedgedriver.exe') driver = webdriver.Edge(service=service, options=edge_options)
3. 修改Edge启动命令,添加必要参数
使用独立的用户数据目录,避免与默认浏览器会话冲突:
"c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222 --user-data-dir="C:\Temp\EdgeDebugProfile" --no-first-run --no-default-browser-check
--user-data-dir指定的目录会自动创建,确保路径有读写权限。
4. 修复Python脚本中的语法错误
youtube.com需要加引号,改为"https://www.youtube.com"- Python中没有
printf函数,改为print
修正后的脚本:
from selenium import webdriver from selenium.webdriver.edge.options import Options from selenium.webdriver.edge.service import Service edge_options = Options() edge_options.add_experimental_option('debuggerAddress', '127.0.0.1:9222') service = Service(r'C:\path\to\msedgedriver.exe') # 替换为你的EdgeDriver路径 driver = webdriver.Edge(service=service, options=edge_options) driver.get("https://www.youtube.com") print(driver.title)
5. 验证调试端口是否正常监听
打开浏览器访问http://127.0.0.1:9222/json,如果能返回JSON格式的会话数据,说明端口正常;如果无法访问,检查启动命令是否正确,或端口是否被其他程序占用。
内容的提问来源于stack exchange,提问作者Justin
相关产品推荐
相关产品推荐

