--------------------------------------------------------------------------- SessionNotCreatedException Traceback (most recent call last) Cell In[14], line 5 2 from selenium.webdriver.common.by import By 3 from time import sleep ----> 5 driver=webdriver.Safari() 6 driver.get('https://movie.douban.com/subject/1291546/') 7 sleep(3) File /opt/anaconda3/lib/python3.12/site-packages/selenium/webdriver/safari/webdriver.py:60, in WebDriver.__init__(self, keep_alive, options, service) 53 executor = SafariRemoteConnection( 54 remote_server_addr=self.service.service_url, 55 keep_alive=keep_alive, 56 ignore_proxy=options._ignore_local_proxy, 57 ) 59 try: ---> 60 super().__init__(command_executor=executor, options=options) 61 except Exception: 62 self.quit() File /opt/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py:250, in WebDriver.__init__(self, command_executor, keep_alive, file_detector, options, locator_converter, web_element_cls, client_config) 248 self._authenticator_id = None 249 self.start_client() --> 250 self.start_session(capabilities) 251 self._fedcm = FedCM(self) 253 self._websocket_connection = None File /opt/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py:342, in WebDriver.start_session(self, capabilities) 333 """Creates a new session with the desired capabilities. 334 335 Parameters: (...) 338 - A capabilities dict to start the session with. 339 """ 341 caps = _create_caps(capabilities) --> 342 response = self.execute(Command.NEW_SESSION, caps)["value"] 343 self.session_id = response.get("sessionId") 344 self.caps = response.get("capabilities") File /opt/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py:429, in WebDriver.execute(self, driver_command, params) 427 response = self.command_executor.execute(driver_comman
时间: 2025-06-21 07:26:08 浏览: 10
<think>好的,我现在需要帮助用户解决Selenium使用Safari时出现的SessionNotCreatedException异常。首先,我得回忆一下Selenium的结构,用户提到的引用里提到common.exceptions包含所有异常,所以这个异常应该属于那里。然后,根据经验,这个异常通常和驱动配置或浏览器设置有关。
首先,用户可能没有启用Safari的自动化选项。我记得Safari需要手动开启开发者菜单里的“允许远程自动化”选项。这个步骤容易忽略,应该作为第一个检查点。
接下来,Selenium的Safari驱动是否需要单独安装?根据引用4,驱动通过command_executor来调度,而Safari可能不需要额外驱动,但需要确认SafariDriver是否已内置。不过,新版本Selenium可能要求使用Safari Technology Preview或者特定版本。用户可能安装了不兼容的Safari或驱动版本,需要检查版本匹配性。
然后,Safari的版本和Selenium的兼容性。比如,Selenium 4.x可能需要Safari 12或更高版本。用户如果用的是旧版Safari,可能会引发异常。建议用户检查并更新浏览器和Selenium库。
另外,权限问题也可能导致异常。用户是否以管理员权限运行脚本?或者Safari的权限设置是否阻止了自动化?需要确保有足够的权限。
还有,启动选项是否正确?比如使用SafariOptions配置,但用户可能没有正确初始化。代码示例可能会有帮助,比如正确导入webdriver并设置选项。
最后,其他因素如浏览器缓存或冲突的扩展程序也可能导致问题。建议用户尝试无痕模式或禁用扩展。
总结下来,解决步骤应该是:启用远程自动化→检查驱动和版本→更新软件→调整权限→正确配置选项→处理其他潜在问题。需要一步步引导用户排查,并给出具体的代码示例和命令。</think>### 逐步解答:解决Selenium使用Safari时的SessionNotCreatedException异常
#### 步骤1:启用Safari的远程自动化功能
1. 打开Safari浏览器
2. 顶部菜单栏选择 **Safari → 偏好设置 → 高级**
3. 勾选 **"在菜单栏中显示开发菜单"**
4. 顶部菜单栏新出现的 **开发** 菜单中选择 **允许远程自动化**[^1]
#### 步骤2:验证环境配置
```python
from selenium import webdriver
# 正确初始化方式
driver = webdriver.Safari()
```
#### 步骤3:版本兼容性检查
- **Selenium版本要求**:需使用≥3.0.0版本
- **Safari版本要求**:需使用≥10.0版本(推荐最新稳定版)
- **驱动验证**:SafariDriver从11版本开始已内置,无需额外安装[^4]
#### 步骤4:权限调整(macOS系统)
```bash
# 终端执行权限配置
sudo safaridriver --enable
```
#### 步骤5:异常处理完整示例
```python
from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException # 引用异常类[^1]
try:
driver = webdriver.Safari()
except SessionNotCreatedException as e:
print(f"异常原因: {e.msg}")
# 建议排查顺序:
# 1. 检查Safari远程自动化是否启用
# 2. 验证safaridriver执行权限
# 3. 升级浏览器和Selenium包
```
#### 其他可能原因及解决方案:
1. **证书信任问题**:系统钥匙串访问 → 将safaridriver证书设为始终信任
2. **端口冲突**:关闭占用4444端口的程序
3. **浏览器插件冲突**:禁用所有Safari扩展程序
阅读全文
相关推荐



