异常提示:无法建立WebSocket连接Selenium ChromeDriver和Chrome v111

jmo0nnb3  于 2023-03-16  发布在  Go
关注(0)|答案(3)|浏览(1673)

bounty将在7天后过期。回答此问题可获得+50的声望奖励。undetected Selenium正在寻找规范答案:从可靠和/或官方来源寻找答案。

我试图调用该网站使用 selenium 和 chrome 浏览器v111。
浏览器正在打开,但网站没有调用。它工作正常,但更新chrome“Version 111.0.5563.65(Official Build)(64位)”后,我得到这个问题:

org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection

我试过了,Eclipse IDE企业Java开发人员(包括孵化组件)版本: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");
     }
 }

请帮我解决这个问题。

4zcjmb1e

4zcjmb1e1#

我按照下面的答案:https://stackoverflow.com/a/75703971/21386874
(“--远程允许源=*”);
我试过了,但是没有用。在我的项目中,我得到了下面的错误。
发生错误:发生异常。无法与以下站点建立WebSocket连接:http://localhost:49877/devtools/browser/3a 3af 47 d-732 a-4337-a91 c-18 c8 ced 545 cd构建信息:版本:“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>

并在@BeforeTest方法的第一行添加了以下代码行:

System.setProperty("webdriver.http.factory", "jdk-http-client");
oymdgrw7

oymdgrw72#

正在使用google-chrome***v111.0***此错误消息...

org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:49877/devtools/browser/3a3af47d-732a-4337-a91c-18c8ced545cd

甚至这个错误消息...

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

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

详情

此问题是 Origin 标头在Selenium当前使用的Netty 4.x中自动解析为无意义值时导致的。此问题在Origin header is always sent from WebSocket client中详细讨论,并通过Fix generating the Origin header value for websocket handshake request解决。
溶液
根据Selenium Blog,有几种方法可以解决这个问题。

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

    • 先决条件 *:
Project configured to use Java 11+
Using Selenium 4.5.0 as a minumum version
    • 集成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>
    • 设置系统属性 *:需要设置系统属性,指示需要使用Java 11+ Http客户端,默认使用 AsyncHttpClient
System.setProperty("webdriver.http.factory", "jdk-http-client");

*在 selenium 中使用java-8*****:正如ChromeDriver verbose log所建议的:

[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.

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

    • java *:
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(options);

参考文献

有用参考的链接:

li9yvcax

li9yvcax3#

ChromeOptions options=new ChromeOptions();    
options.addArguments("--remote-allow-origins=*");    
//Launching the browser
driver=new ChromeDriver(options);

相关问题