org.openqa.selenium.remote.http.ConnectionFailedException:无法建立 websocket 连接 Selenium ChromeDriver 和 Chrome v111

JBM*_*JBM 8 java google-chrome selenium-chromedriver selenium-webdriver selenium-java

我尝试使用 Selenium 和 Chrome 浏览器 v111 调用该网站。

浏览器正在打开,但网站未调用。它工作正常,但更新 chrome“版本 111.0.5563.65(官方版本)(64 位)”后,我遇到了这个问题:

org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection
Run Code Online (Sandbox Code Playgroud)

我尝试过,Eclipse IDE for Enterprise Java Developers(包括孵化组件)版本:2020-12(4.18.0)构建ID:20201210-1552。

这是代码:

 package com.testng.library_Files;

 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.chrome.ChromeOptions;
 import org.testng.annotations.Test;

 public class one {
     WebDriver driver=null;

     @Test(priority = 1)
     public void DoSetup()
     {
         //System.setProperty("webdriver.chrome.driver","./src/main/java/drivers/chromedriver.exe");
         ChromeOptions options= new ChromeOptions(); 
         options.setHeadless(true);
         //driver= new ChromeDriver(options);
         driver= new ChromeDriver();
     }

     @Test(priority = 2)
     public void LaunchURL()
     {
         driver.get("https://www.google.com");
     }
 }
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题。

Deb*_*anB 19

使用 v111.0此错误消息...

org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:49877/devtools/browser/3a3af47d-732a-4337-a91c-18c8ced545cd
Run Code Online (Sandbox Code Playgroud)

还有这个错误信息...

2023-03-08T21:06:50.3319163Z WARNING: Invalid Status code=403 text=Forbidden
2023-03-08T21:06:50.3320374Z java.io.IOException: Invalid Status code=403 text=Forbidden
Run Code Online (Sandbox Code Playgroud)

甚至这个错误消息......

java.lang.NullPointerException: Cannot invoke "org.asynchttpclient.ws.WebSocket.sendCloseFrame(int, String)" because "this.socket" is null
Run Code Online (Sandbox Code Playgroud)

devtools_http_handler...是拒绝来自 http://localhost 源的传入 WebSocket 连接的结果。


细节

此问题是在 Selenium 当前使用的Netty 4.xOrigin中 set 自动解析为无意义值时 header的结果。此问题已在Origin header is always sent from WebSocket client 中详细讨论,并已通过修复为 websocket 握手请求生成 Origin header 值来解决。


解决方案

根据Selenium 博客,有几种方法可以解决这个问题。

  • 在 Selenium 中使用 HTTP 客户端:Selenium 使用 HTTP 客户端和关联的 WebSocket 客户端来实现多种目的。AsyncHttpClient是一个构建在 Netty 之上的开源库。它允许异步执行 HTTP 请求和响应。此外,它还提供 WebSocket 支持。但自 2021 年 6 月起, AsyncHttpClient不再维护,因为Java 11+提供了内置的 HTTP 和 WebSocket 客户端。Selenium 可以利用它来替代 AsyncHttpClient。

    • 先决条件

      Project configured to use Java 11+
      Using Selenium 4.5.0 as a minumum version
      
      Run Code Online (Sandbox Code Playgroud)
    • 集成 Java 11+ 客户端:由于 Java 11+ HTTP 客户端位于自己的工件中,因此可以将其导入到使用 Java 11+ 的项目中,如下所示:

      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.5.0</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-http-jdk-client</artifactId>
        <version>4.5.0</version>
      </dependency>
      
      Run Code Online (Sandbox Code Playgroud)
    • 设置系统属性:您需要设置系统属性以指示需要使用Java 11+ Http客户端。默认情况下,它使用AsyncHttpClient

      System.setProperty("webdriver.http.factory", "jdk-http-client");
      
      Run Code Online (Sandbox Code Playgroud)
  • 在 Selenium 中使用 :正如 ChromeDriver详细日志所示:

    [32332:259:0214/190812.204658:ERROR:devtools_http_handler.cc(766)] Rejected an incoming WebSocket connection from the http://localhost:58642 origin. Use the command line flag --remote-allow-origins=http://localhost:58642 to allow connections from this origin or --remote-allow-origins=* to allow all origins.
    
    Run Code Online (Sandbox Code Playgroud)

解决此问题的一个快速方法是添加参数,--remote-allow-origins=*如下所示:


参考

有用参考的链接:


JBM*_*JBM 10

我遵循以下答案:/sf/answers/5299278001/

options.addArguments("--remote-allow-origins=*"); 
Run Code Online (Sandbox Code Playgroud)

我尝试过,但它不起作用。在我的项目中,我收到以下错误。
错误:

org.openqa.selenium.remote.http.ConnectionFailedException:无法建立到 http://localhost:49877/devtools/browser/3a3af47d-732a-4337-a91c-18c8ced545cd 的 Websocket 连接构建信息:版本:'4.5.3',修订版:“4b786a1e430”

答:

我下载了最新的 chromedriver.exe 版本为 111.0.5563.64。另外,我还添加了一个依赖项:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-http-jdk-client</artifactId>
  <version>4.5.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

并将此代码行添加到方法的第一行@BeforeTest

System.setProperty("webdriver.http.factory", "jdk-http-client");
Run Code Online (Sandbox Code Playgroud)