Java环境初始化FirefoxDriver遇SessionNotCreatedException问题求助
问题排查:FirefoxDriver初始化触发SessionNotCreatedException
环境信息
- Java:22版本
- Geckodriver:34版本
- Selenium JAR包:4.18.1版本
- Firefox浏览器:ESR 115.9.1(最新)
- IDE:IntelliJ
问题现象
初始化FirefoxDriver时触发SessionNotCreatedException异常,浏览器短暂打开后崩溃退出。
错误日志
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. Host info: host: 'MATTHEWLAPTOP', ip: '141.219.236.17' Build info: version: '4.18.1', revision: 'b1d3319b48' System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '22' Driver info: org.openqa.selenium.firefox.FirefoxDriver Command: [null, newSession {capabilities=[Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {}}]}] at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:537) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:233) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:162) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:155) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:150) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:133) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:128) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:113) at Main.main(Main.java:88) Caused by: java.io.UncheckedIOException: java.io.IOException: Cannot run program "geckodriver.exe": CreateProcess error=216, This version of %1 is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher at org.openqa.selenium.os.ExternalProcess$Builder.start(ExternalProcess.java:193) at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:207) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:117) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:519) ... 8 more Caused by: java.io.IOException: Cannot run program "geckodriver.exe": CreateProcess error=216, This version of %1 is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1170) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1089) at org.openqa.selenium.os.ExternalProcess$Builder.start(ExternalProcess.java:191) ... 11 more Caused by: java.io.IOException: CreateProcess error=216, This version of %1 is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher at java.base/java.lang.ProcessImpl.create(Native Method) at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:500) at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:159) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1126) ... 13 more Process finished with exit code 1
异常触发源码
异常触发于RemoteWebDriver.java的execute方法第537行,相关代码如下:
protected Response execute(CommandPayload payload) { Command command = new Command(sessionId, payload); Response response; long start = System.currentTimeMillis(); String currentName = Thread.currentThread().getName(); Thread.currentThread() .setName( String.format("Forwarding %s on session %s to remote", command.getName(), sessionId)); try { log(sessionId, command.getName(), command, When.BEFORE); response = executor.execute(command); log(sessionId, command.getName(), response, When.AFTER); if (response == null) { return null; } // Unwrap the response value by converting any JSON objects of the form // {"ELEMENT": id} to RemoteWebElements. Object value = getElementConverter().apply(response.getValue()); response.setValue(value); } catch (Throwable e) { log(sessionId, command.getName(), e.getMessage(), When.EXCEPTION); WebDriverException toThrow; if (command.getName().equals(DriverCommand.NEW_SESSION)) { if (e instanceof SessionNotCreatedException) { toThrow = (WebDriverException) e; } else { toThrow = //(And this is line 537 where the error happened) new SessionNotCreatedException( "Possible causes are invalid address of the remote server or browser start-up" + " failure.", e); } } else if (e instanceof WebDriverException) { toThrow = (WebDriverException) e; } else { toThrow = new UnreachableBrowserException( "Error communicating with the remote browser. It may have died.", e); } populateWebDriverException(toThrow); // Showing full command information when user is debugging // Avoid leaking user/pwd values for authenticated Grids. if (toThrow instanceof UnreachableBrowserException && !Debug.isDebugging()) { toThrow.addInfo( "Command", "[" + command.getSessionId() + ", " + command.getName() + " " + command.getParameters().keySet() + "]"); } else { toThrow.addInfo("Command", command.toString()); } throw toThrow; } finally { Thread.currentThread().setName(currentName); } try { errorHandler.throwIfResponseFailed(response, System.currentTimeMillis() - start); } catch (WebDriverException ex) { populateWebDriverException(ex); ex.addInfo("Command", command.toString()); throw ex; } return response; }
测试代码
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; public class Main { public static void main(String[] args) { String pathOfFireFoxDriver = "geckodriver.exe"; FirefoxOptions options = new FirefoxOptions(); System.setProperty("webdriver.gecko.driver", pathOfFireFoxDriver); FirefoxDriver fireFoxDriver = new FirefoxDriver(options); } }
原因分析
从错误日志的根因*CreateProcess error=216, This version of %1 is not compatible with the version of Windows you're running*可以明确:当前使用的Geckodriver 34版本与Windows系统架构不兼容,大概率是下载了错误的架构版本(比如32位Geckodriver在64位Windows 11上运行)。虽然你确认了组件版本兼容,但系统架构不匹配属于版本兼容之外的系统适配问题,会直接导致Geckodriver启动失败,进而触发SessionNotCreatedException。
解决办法
- 卸载当前Geckodriver 34版本,重新下载对应Windows x64架构的Geckodriver 34版本
- 验证Geckodriver兼容性:直接双击运行
geckodriver.exe,若弹出版本不兼容提示则说明架构错误 - 优化路径配置:测试代码中直接使用
"geckodriver.exe"需确保文件在项目根目录或系统PATH中,建议改用绝对路径避免路径问题 - 若问题仍存在,尝试升级Geckodriver到与Firefox ESR 115.9.1匹配的最新兼容版本,确保组件间的适配性
内容的提问来源于stack exchange,提问作者Matthew Staats
相关产品推荐
相关产品推荐

