Edge驱动版本不被识别问题排查(Selenium+Edge环境)
我开发了一款自定义应用,通过Selenium和Microsoft Edge验证网站登录是否成功,并生成登录成功与失败的网站报告。技术栈为Java、Spring Boot、Maven、Selenium及Microsoft Edge。
近期更新服务器上的Microsoft Edge浏览器版本后,已同步更新Edge驱动以匹配版本,但仍收到驱动版本未知的错误,应用此前长期正常运行。
错误信息
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: NettyHttpHandler request execution error Host info:
System info: os.name: 'Windows Server 2022', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_361' Driver info: driver.version: unknown
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:96)
at org.openqa.selenium.remote.RemoteWebDriverBuilder.getRemoteDriver(RemoteWebDriverBuilder.java:396)
at org.openqa.selenium.remote.RemoteWebDriverBuilder.build(RemoteWebDriverBuilder.java:368)
at portal.support.automatedportalcheck.job.SeleniumLoginTestMergedAndNotifyJob.setup(SeleniumLoginTestMergedAndNotifyJob.java:299)
at portal.support.automatedportalcheck.job.SeleniumLoginTestMergedAndNotifyJob.execute(SeleniumLoginTestMergedAndNotifyJob.java:88)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
代码实现
以下是创建Edge Driver的代码(prop.getSeleniumExe()返回Microsoft Edge Driver的路径):
private WebDriver setup(EdgeDriverService service) { System.setProperty("webdriver.edge.driver", prop.getSeleniumExe()); EdgeOptions options = new EdgeOptions(); if(!this.prop.isTrustedConnection()) { options.addArguments("--ignore-certificate-errors"); options.addArguments("--ignore-ssl-errors"); } options.addArguments("--headless"); options.addArguments("--disable-gpu"); options.addArguments("--disable-extensions"); options.setPageLoadTimeout(Duration.ofSeconds(60)); options.setScriptTimeout(Duration.ofSeconds(30)); options.setImplicitWaitTimeout(Duration.ofSeconds(30)); try { service.start(); } catch (IOException e) { log.debug(e.getMessage()); } ClientConfig clientConfig = ClientConfig.defaultConfig().readTimeout(Duration.ofSeconds(30)); WebDriver driver = RemoteWebDriver.builder() .oneOf(options) .withDriverService(service) .config(clientConfig) .build(); return driver; }
严格匹配驱动与浏览器版本
打开Edge浏览器输入edge://version/查看完整版本号,下载完全一致版本的EdgeDriver(注意对应服务器的amd64架构),替换现有驱动文件,确保驱动路径正确且文件有执行权限。调整DriverService初始化逻辑
避免同时使用System.setProperty和传入的EdgeDriverService,建议显式构建Service:EdgeDriverService service = new EdgeDriverService.Builder() .usingDriverExecutable(new File(prop.getSeleniumExe())) .usingAnyFreePort() .build();同时,启动Service时捕获完整错误日志,避免静默失败:
try { service.start(); } catch (IOException e) { log.error("EdgeDriverService启动失败", e); throw new RuntimeException("无法启动Edge驱动", e); }更新Headless模式参数
Edge新版本对Headless模式的参数有调整,将--headless替换为--headless=new,同时添加窗口尺寸参数:options.addArguments("--headless=new"); options.addArguments("--window-size=1920,1080");检查Java与Selenium版本兼容性
Java 8对Selenium 4.x部分版本支持有限,若允许可升级至Java 11;若需保留Java 8,确认使用兼容的Selenium稳定版本(如4.5.0)。清理临时文件与指定用户目录
为Edge指定临时用户目录,避免缓存或旧配置干扰:options.addArguments("--user-data-dir=C:\\temp\\edge-temp-profile"); options.addArguments("--disable-dev-shm-usage");
内容的提问来源于stack exchange,提问作者Victor Will

