Edge浏览器IE模式自动化脚本最小化/锁屏时启动失败求助
Edge IE模式自动化脚本锁屏/最小化时超时问题排查
问题场景
使用Edge浏览器的IE模式测试Web自动化脚本,搭配32位4.8.1版本IEDriver与Selenium 4.0.1。脚本在正常窗口状态下运行无异常,但屏幕最小化或锁屏时,浏览器会卡在初始化页面,抛出SessionNotCreatedException超时错误。
相关代码
public class AlertPopUp { public static void main(String[] args) { System.setProperty("webdriver.ie.driver", "D:\\AlertPopUp\\IEDriverServer.exe"); RemoteWebDriver driver = null; try { InternetExplorerOptions ieOptions = new InternetExplorerOptions(); ieOptions.attachToEdgeChrome(); ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"); ieOptions.requireWindowFocus(); ieOptions.setCapability("ignoreProtectedModeSettings", true); ieOptions.ignoreZoomSettings(); ieOptions.destructivelyEnsureCleanSession(); ieOptions.setCapability (InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false) ; ieOptions.setCapability ("disable-popup-blocking", true); driver = new InternetExplorerDriver(ieOptions); driver.manage().window().maximize(); driver.get("https://www.google.com/"); driver.quit(); } catch (Exception e) { e.printStackTrace(); } } }
报错信息
Started InternetExplorerDriver server (32-bit) 4.8.1.0 Listening on port 29726 Only local connections are allowed 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: 'AJAX', ip: '192.168.0.106' Build info: version: '4.8.0', revision: '267030adea' System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.4.1' Driver info: org.openqa.selenium.ie.InternetExplorerDriver Command: [null, newSession {capabilities=[Capabilities {browserName: internet explorer, disable-popup-blocking: true, se:ieOptions: {enablePersistentHover: false, ie.edgechromium: true, ie.edgepath: C:\Program Files (x86)\Micr..., ignoreProtectedModeSettings: true, ignoreZoomSetting: true, requireWindowFocus: true}}], desiredCapabilities=Capabilities {browserName: internet explorer, disable-popup-blocking: true, se:ieOptions: {enablePersistentHover: false, ie.edgechromium: true, ie.edgepath: C:\Program Files (x86)\Micr..., ignoreProtectedModeSettings: true, ignoreZoomSetting: true, requireWindowFocus: true}}} Capabilities {} at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:561) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229) at org.openqa.selenium.ie.InternetExplorerDriver.run(InternetExplorerDriver.java:153) at org.openqa.selenium.ie.InternetExplorerDriver.(InternetExplorerDriver.java:140) at org.openqa.selenium.ie.InternetExplorerDriver.(InternetExplorerDriver.java:117) at testpopup.AlertPopUp.main(AlertPopUp.java:42) 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:56) at org.openqa.selenium.remote.http.netty.NettyHttpHandler.execute(NettyHttpHandler.java:49) 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:56) at org.openqa.selenium.remote.http.netty.NettyClient.execute(NettyClient.java:99) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:124) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:106) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543) ... 5 more Caused by: java.util.concurrent.ExecutionException: java.util.concurrent.TimeoutException: Request timeout to localhost/127.0.0.1:29726 after 180000 ms at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396) at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2096) at org.asynchttpclient.netty.NettyResponseFuture.get(NettyResponseFuture.java:206) at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:59) ... 18 more Caused by: java.util.concurrent.TimeoutException: Request timeout to localhost/127.0.0.1:29726 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:833)
浏览器卡住状态截图

解决方案
1. 移除requireWindowFocus()配置
代码中启用的ieOptions.requireWindowFocus()要求浏览器必须获得前台窗口焦点才能交互,锁屏或最小化时系统会剥夺焦点,导致IEDriver无法完成会话初始化。修改代码:
// 注释或删除该行 // ieOptions.requireWindowFocus();
2. 启用Edge无头模式适配
添加Edge无头模式参数,避免窗口状态影响脚本运行:
ieOptions.addArguments("--headless=new"); ieOptions.addArguments("--disable-gpu");
3. 调整系统电源设置
若必须保留焦点需求,修改Windows电源设置防止锁屏时关闭显示:
- 打开「控制面板」→「电源选项」→「更改计划设置」→「更改高级电源设置」
- 展开「显示」→「控制台锁定显示关闭超时」,设置为0(永不关闭)
4. 使用虚拟/远程桌面会话
通过Hyper-V虚拟机或远程桌面运行脚本,虚拟会话中的桌面始终保持活跃,不会因本地锁屏失去焦点。
内容的提问来源于stack exchange,提问作者340- Surwade Ajay Mukund
相关产品推荐
相关产品推荐

