Windows11下Selenium Java调用Edge IE兼容模式无响应异常问题
问题分析与解决方案
问题重现
运行以下Selenium Java代码后,浏览器启动并导航至Bing.com,但随后无响应,最终抛出UnreachableBrowserException超时错误:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.ie.InternetExplorerOptions; public class Main { public static void main(String[] args) { InternetExplorerOptions ieOptions = new InternetExplorerOptions(); ieOptions.attachToEdgeChrome(); ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"); WebDriver driver = new InternetExplorerDriver(ieOptions); driver.get("http://www.bing.com"); driver.findElement(By.id("sb_form_q")).sendKeys("Elon Musk"); } }
错误日志
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died. Build info: version: '4.10.0', revision: 'c14d967899' System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '23.0.1' Driver info: org.openqa.selenium.ie.InternetExplorerDriver Command: [f149ad19-2825-4c0d-a9fb-539b4a1130bb, get {url=http://www.bing.com}] Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.edgechromium: true, ie.edgepath: C:\Program Files (x86)\Micr..., ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ie.ignoreprocessmatch: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: true, initialBrowserUrl: http://localhost:15552/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify} Session ID: f149ad19-2825-4c0d-a9fb-539b4a1130bb at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:558) at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:295) at org.example.Main.main(Main.java:15) Caused by: java.lang.RuntimeException: NettyHttpHandler request execution error at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:76) at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42) at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:55) at org.openqa.selenium.remote.http.netty.NettyHttpHandler.execute(NettyHttpHandler.java:48) at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42) at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:55) at org.openqa.selenium.remote.http.netty.NettyClient.execute(NettyClient.java:97) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:188) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:531) ... 2 more Caused by: java.util.concurrent.ExecutionException: java.util.concurrent.TimeoutException: Request timeout to localhost/127.0.0.1:15552 after 180000 ms at java.base/java.util.concurrent.CompletableFuture.wrapInExecutionException(CompletableFuture.java:345) at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:440) at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2140) at org.asynchttpclient.netty.NettyResponseFuture.get(NettyResponseFuture.java:206) at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:59) ... 12 more Caused by: java.util.concurrent.TimeoutException: Request timeout to localhost/127.0.0.1:15552 after 180000 ms at org.asynchttpclient.netty.timeout.TimeoutTimerTask.expire(TimeoutTimerTask.java:43) at org.asynchttpclient.netty.timeout.RequestTimeoutTimerTask.run(RequestTimeoutTimerTask.java:50) at io.netty.util.HashedWheelTimer$HashedWheelTimeout.run(HashedWheelTimer.java:715) at io.netty.util.concurrent.ImmediateExecutor.execute(ImmediateExecutor.java:34) at io.netty.util.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:703) at io.netty.util.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:790) at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:503) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:1575)
预期行为:浏览器正常响应,完成搜索框文本输入操作。
问题原因
- 驱动选型不当:使用
InternetExplorerDriver附加Edge Chromium属于兼容模式,并非官方推荐的标准方式,在高版本Selenium和Java环境下易出现兼容性问题。 - 配置缺失:
browserAttachTimeout设置为0,驱动等待浏览器响应的时间过短;未启用forceCreateProcessApi,影响驱动与Edge进程的通信稳定性。 - 版本兼容性:Selenium 4.10.0与Java 23.0.1的组合存在适配隐患,加上IEDriverServer与Edge版本不匹配,进一步导致通信超时。
解决方案
方案1:改用官方EdgeDriver驱动(推荐)
直接使用EdgeDriver驱动Edge Chromium,这是官方标准方式,稳定性和兼容性更佳:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.edge.EdgeOptions; public class Main { public static void main(String[] args) { EdgeOptions edgeOptions = new EdgeOptions(); // 若Edge路径未被自动检测,可手动指定(默认无需设置) edgeOptions.setBinary("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"); WebDriver driver = new EdgeDriver(edgeOptions); driver.get("http://www.bing.com"); // 隐式等待确保元素加载完成 driver.manage().timeouts().implicitlyWait(java.time.Duration.ofSeconds(10)); driver.findElement(By.id("sb_form_q")).sendKeys("Elon Musk"); } }
方案2:调整IE Options配置(仅适用于必须使用IE Driver的场景)
若必须通过IE Driver附加Edge,补充以下配置优化通信稳定性:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.ie.InternetExplorerOptions; public class Main { public static void main(String[] args) { InternetExplorerOptions ieOptions = new InternetExplorerOptions(); ieOptions.attachToEdgeChrome(); ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"); // 设置浏览器附加超时时间 ieOptions.setBrowserAttachTimeout(java.time.Duration.ofSeconds(30)); // 启用强制创建进程API,提升通信稳定性 ieOptions.forceCreateProcessApi(); // 忽略进程匹配,避免驱动误判浏览器进程 ieOptions.ignoreProcessMatch(); WebDriver driver = new InternetExplorerDriver(ieOptions); driver.get("http://www.bing.com"); driver.manage().timeouts().implicitlyWait(java.time.Duration.ofSeconds(10)); driver.findElement(By.id("sb_form_q")).sendKeys("Elon Musk"); } }
额外注意事项
- 确保IEDriverServer版本与Selenium版本完全匹配,且Edge浏览器版本与驱动兼容。
- 优先使用Java LTS版本(如Java 17),避免过高版本Java带来的适配问题。
- 添加等待机制,确保页面元素加载完成后再执行操作,避免因元素未就绪引发异常。
内容的提问来源于stack exchange,提问作者Saadia
相关产品推荐
相关产品推荐

