org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection Selenium ChromeDriver and Chrome v111
Asked Answered
G

7

8

I tried to invoke the website using Selenium and Chrome browser v111.

The browser is opening but the website is not invoking. It is working properly but after updating chrome "Version 111.0.5563.65 (Official Build) (64-bit)" I'm getting this problem:

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

I tried, Eclipse IDE for Enterprise Java Developers (includes Incubating components) Version: 2020-12 (4.18.0) Build id: 20201210-1552.

This is the code:

 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");
     }
 }

Please help me to solve this issue.

Gyniatrics answered 13/3, 2023 at 5:51 Comment(1)
what issue? what problem?Goosefoot
G
11

I followed the below answer: https://mcmap.net/q/413115/-unable-to-establish-websocket-connection

options.addArguments("--remote-allow-origins=*"); 

I tried but it is not working. In my project, I got the below error.
Error:

org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:49877/devtools/browser/3a3af47d-732a-4337-a91c-18c8ced545cd Build info: version: '4.5.3', revision: '4b786a1e430'

Ans:

I downloaded the newest chromedriver.exe with the 111.0.5563.64 version. Also, I added one more dependency:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-http-jdk-client</artifactId>
  <version>4.5.0</version>
</dependency>

And added this code line at the first line into the @BeforeTest method:

System.setProperty("webdriver.http.factory", "jdk-http-client");
Gyniatrics answered 13/3, 2023 at 12:44 Comment(0)
P
21

Using v111.0 this error message...

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

and this error message...

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

and even this error message...

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

...is the result of devtools_http_handler rejecting an incoming WebSocket connection from the http://localhost origin.


Details

This issue is the result of Origin header when set is being auto-resolved into meaningless value in Netty 4.x currently being used by Selenium. This issue was discussed in details in Origin header is always sent from WebSocket client and was addressed through Fix generating the Origin header value for websocket handshake request.


Solution

As per the Selenium Blog there are a couple of approaches to solve this issue.

  • Using HTTP Client in Selenium: Selenium uses an HTTP client and associated WebSocket client for multiple purposes. AsyncHttpClient is an open-source library built on top of Netty. It allows the execution of HTTP requests and responses asynchronously. Additionally it also provides WebSocket support.But AsyncHttpClient is no more maintained since June 2021 as Java 11+ provides a built-in HTTP and WebSocket client. Selenium can utilize it to replace AsyncHttpClient.

    • Prerequisites:

      Project configured to use Java 11+
      Using Selenium 4.5.0 as a minumum version
      
    • Integrating the Java 11+ client: As Java 11+ HTTP client sits in its own artifact it can be imported into your project that use Java 11+ as follows:

      <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>
      
    • Setting the system property: You need to set the system property to indicate that Java 11+ Http client needs to be used. By default, it uses the AsyncHttpClient:

      System.setProperty("webdriver.http.factory", "jdk-http-client");
      
  • Using in Selenium: As the ChromeDriver verbose log suggests:

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

A quick fix to this issue would be to add the argument --remote-allow-origins=* as follows:

  • Java:

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

References

Links to useful references:

Prefecture answered 15/3, 2023 at 7:42 Comment(0)
G
11

I followed the below answer: https://mcmap.net/q/413115/-unable-to-establish-websocket-connection

options.addArguments("--remote-allow-origins=*"); 

I tried but it is not working. In my project, I got the below error.
Error:

org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:49877/devtools/browser/3a3af47d-732a-4337-a91c-18c8ced545cd Build info: version: '4.5.3', revision: '4b786a1e430'

Ans:

I downloaded the newest chromedriver.exe with the 111.0.5563.64 version. Also, I added one more dependency:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-http-jdk-client</artifactId>
  <version>4.5.0</version>
</dependency>

And added this code line at the first line into the @BeforeTest method:

System.setProperty("webdriver.http.factory", "jdk-http-client");
Gyniatrics answered 13/3, 2023 at 12:44 Comment(0)
L
5
ChromeOptions options=new ChromeOptions();    
options.addArguments("--remote-allow-origins=*");    
//Launching the browser
driver=new ChromeDriver(options);
Lennalennard answered 13/3, 2023 at 9:0 Comment(0)
H
3

I faced same issue and found solutions. I added below code then program worked perfectly.

ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(options);
Himyaritic answered 7/6, 2023 at 4:25 Comment(0)
R
2

I downloaded chromedriver_win32.zip for ChromeDriver 111.0.5563.64 version & on workspace I added below code.

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

After this issue resolved

Requital answered 25/3, 2023 at 3:33 Comment(0)
G
0

I was facing the same error for different versions and this got resolved for me. The problem might be with the java, selenium and testNG versions compatibility. You can try with java 11 , selenium version as 4.5 or above and testNG version as 7.5

Galvani answered 20/3, 2023 at 13:20 Comment(0)
S
0

In Chrome 117 there is an issue, that could be solved by adding a dependency:

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.8.2</version>
</dependency>
Stillmann answered 6/10, 2023 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.