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");
}
}
当我执行提供的代码时,网络浏览器通过 selenium 成功启动并导航到 Bing.com。然而,浏览器随后变得无响应,无法关注搜索字段并输入“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)
浏览器不应无响应并输入提供的文本
这是 Internet Explorer 安全区域的保护模式设置问题。
在使用 Java 中的 Selenium 进行 Internet Explorer (IE) 自动化的安全区域中,必须确保保护模式设置在所有区域中保持一致。在 Windows 11 中,默认情况下仅为区域 1 设置保护模式。
默认情况下,定义了以下五个区域:
0: My Computer
1: Local Intranet Zone
2: Trusted Sites Zone
3: Internet Zone
4: Restricted Sites Zone
注意: 每个区域的保护模式通常是通过 Internet 选项设置的。默认情况下,“我的电脑”不会出现在“安全”选项卡上的“区域”框中,因为它已被锁定以帮助提高安全性。
所有区域的保护模式设置必须一致。如果不是,您将遇到以下异常:
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.
鉴于 Microsoft 已停止使用 IE,保护模式设置已从 Windows 11 中的 Internet 选项中删除。因此,您可以使用注册表启用保护模式。为此,请为保护模式创建或设置名为 2500 的 DWORD。值 0 表示保护模式已启用,而值 3 表示保护模式已禁用。
以下是创建保护模式 DWORD 的步骤:
Press Windows + R to open the Run window.
Type regedit.
Press Enter.
Navigate to the following path: Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
For Zone 1, DWORD 2500 is already created. Update its value to 0.
Click on Zone 2.
Right-click in the right panel, select New > DWORD (32-bit) Value.
Set the name to 2500 and the value to 0.
Repeat steps 6 to 8 for all other zones.