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

更新Firefox、geckodriver和Selenium后出现SessionNotCreatedException

Selenium SessionNotCreatedException 问题解决

环境版本

  • geckodriver:v0.33.0
  • Firefox:115.0
  • Selenium Webdrivers:4.10.0

报错信息

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

原代码

ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("newProfile");
myprofile.setPreference("dom.webnotifications.enabled", false);

FirefoxOptions options = new FirefoxOptions();
options.setCapability("firefox_binary", "c:\\Program Files\\Mozilla Firefox\\firefox.exe");
options.setProfile(myprofile);

System.setProperty("webdriver.gecko.driver", "C:\\Users\\me\\Downloads\\geckodriver\\geckodriver.exe");

WebDriver driver = new FirefoxDriver(options);

driver.get(websiteURL);

问题原因及解决步骤

1. 过时API兼容性问题

Selenium 4.x 对Firefox配置逻辑有更新,旧的ProfilesIni加载配置文件、setCapability("firefox_binary")设置浏览器路径的方式,在高版本Firefox下可能触发启动失败:

  • 用options.setBinary()替代setCapability("firefox_binary")
  • 优先通过FirefoxOptions.addPreference()直接配置浏览器偏好,减少对旧FirefoxProfileAPI的依赖

2. 驱动属性设置顺序错误

System.setProperty("webdriver.gecko.driver", ...)需要在初始化FirefoxDriver之前执行,确保驱动路径被正确识别。

修改后的代码示例

// 先设置geckodriver路径
System.setProperty("webdriver.gecko.driver", "C:\\Users\\me\\Downloads\\geckodriver\\geckodriver.exe");

// 初始化FirefoxOptions并配置
FirefoxOptions options = new FirefoxOptions();
// 设置Firefox安装路径
options.setBinary("c:\\Program Files\\Mozilla Firefox\\firefox.exe");
// 直接添加偏好设置
options.addPreference("dom.webnotifications.enabled", false);

// 若需使用现有Firefox配置文件,替换为以下代码(需替换为实际profile文件夹路径)
// File profileDir = new File("C:\\Users\\你的用户名\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\xxxx.newProfile");
// FirefoxProfile myprofile = new FirefoxProfile(profileDir);
// options.setProfile(myprofile);

// 启动驱动
WebDriver driver = new FirefoxDriver(options);
driver.get(websiteURL);

额外检查项

  • 确认geckodriver路径无拼写错误,文件存在且可正常执行
  • Firefox 115 ESR与geckodriver 0.33.0版本兼容,无需调整版本

内容的提问来源于stack exchange,提问作者ratsstack

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 03:07:48