Traceback (most recent call last): File "d:\Workplace\Python\test2.py", line 19, in <module> driver = webdriver.Edge(service=service, options=edge_options) File "D:\python\Lib\site-packages\selenium\webdriver\edge\webdriver.py", line 45, in __init__ super().__init__( ~~~~~~~~~~~~~~~~^ browser_name=DesiredCapabilities.EDGE["browserName"], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<3 lines>... keep_alive=keep_alive, ^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\python\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 67, in __init__ super().__init__(command_executor=executor, options=options) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 260, in __init__ self.start_session(capabilities) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 357, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 448, in execute self.error_handler.check_response(response) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^ File "D:\python\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir Stacktrace: GetHandleVerifier [0x00007FF7EC96FCA5+25029] (No symbol) [0x00007FF7EC8C4CB0] Microsoft::Applications::Events::EventProperty::to_string [0x00007FF7ECBCADAA+1947706] (No symbol) [0x00007FF7EC6C280D] (No symbol) [0x00007FF7EC6C03B3] (No symbol) [0x00007FF7EC7052CD] (No symbol) [0x00007FF7EC7043CB] (No symbol) [0x00007FF7EC6F8DF3] (No symbol) [0x00007FF7EC6CD6A6] (No symbol) [0x00007FF7EC6CCBB3] (No symbol) [0x00007FF7EC6CD4D3] (No symbol) [0x00007FF7EC7D5D0D] (No symbol) [0x00007FF7EC7E38AF] (No symbol) [0x00007FF7EC7DC29F] Microsoft::Applications::Events::EventProperty::to_string [0x00007FF7ECA34D5A+284650] (No symbol) [0x00007FF7EC8D2641] (No symbol) [0x00007FF7EC8CB0E4] (No symbol) [0x00007FF7EC8CB233] (No symbol) [0x00007FF7EC8BCE76] BaseThreadInitThunk [0x00007FFF5E02E8D7+23] RtlUserThreadStart [0x00007FFF5E61C5DC+44]
时间: 2025-05-27 19:33:40 浏览: 33
### 解决 Selenium Edge 浏览器启动时出现的 `SessionNotCreatedException` 错误
当尝试通过 Selenium 启动 Microsoft Edge 浏览器时,如果遇到错误 `selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of MSEdgeDriver only supports MSEdge version 102`, 这通常是因为浏览器驱动程序 (MSEdgeDriver) 和实际安装的 Edge 浏览器版本不匹配所致[^1]。
以下是可能导致此问题的原因以及解决方案:
#### 原因分析
1. **Edge 驱动版本与浏览器版本不符**: 如果使用的 MSEdgeDriver 版本支持特定范围内的 Edge 浏览器版本,而当前机器上的 Edge 浏览器已更新到更高版本,则会触发该异常。
2. **用户数据目录冲突**: 当多个实例试图访问同一个用户配置文件 (`User Data Directory`) 或者未正确关闭之前的浏览器进程时,也可能引发类似的错误消息。
3. **权限不足或其他环境因素**: 文件系统的读写权限受限或者某些杀毒软件阻止了对临时文件夹的操作也可能是原因之一。
#### 解决方案
为了有效处理这个问题可以从以下几个方面入手:
1. **确认并下载对应版本的 MSEdgeDriver**
- 访问官方站点 https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ 查看最新发布的兼容性列表,并依据本地计算机所装设之Microsoft Edge的实际版本号来选取相适配的webdriver版本.
2. **设置新的 User Data Directory 参数**
使用命令行参数指定不同的用户资料路径可以避免重复使用同一份档案造成干扰。例如,在初始化 WebDriver 实例前加入以下选项:
```python
from selenium import webdriver
from selenium.webdriver.edge.service import Service
options = webdriver.EdgeOptions()
options.add_argument("--user-data-dir=C:\\Path\\To\\Custom\\Profile") # 设置自定义用户数据目录
service = Service(executable_path="path/to/msedgedriver.exe")
driver = webdriver.Edge(service=service, options=options)
```
3. **清理残留进程**
确保先前执行过程中遗留下来的任何旧版edge窗口已被完全终止掉再重新开启新测试流程;可以通过任务管理器手动结束所有相关联的服务项或是利用脚本来实现自动化清除功能。
4. **升级至最新稳定发行版**
定期检查是否有可用的新版本发布可供下载应用从而保持工具链始终处于良好状态之下减少不必要的麻烦发生几率.
---
```python
from selenium import webdriver
from selenium.webdriver.edge.service import Service
# Define path variables accordingly on your system setup
ms_edge_driver_path = r"C:\WebDrivers\msedgedriver.exe"
custom_user_data_dir = r"C:\Temp\SeleniumProfiles"
options = webdriver.EdgeOptions()
options.add_argument(f"--user-data-dir={custom_user_data_dir}")
service = Service(ms_edge_driver_path)
try:
driver = webdriver.Edge(service=service, options=options)
finally:
if 'driver' in locals():
driver.quit() # Ensure proper cleanup after execution completes or fails unexpectedly
```
阅读全文
相关推荐



















