Introduction
You might get some If you’ve recently updated your Chrome browser to version 115. x, you might encounter the “org.openqa.selenium.remote.http.ConnectionFailedException” exception.
you may also come across another issue that is “org.openqa.selenium.SessionNotCreatedException“.
Here’s a solution to quickly address these issues.
Errors
The errors are:
Error 1 | org.openqa.selenium.remote.http.ConnectionFailedException
org.openqa.selenium.remote.http.ConnectionFailedException
Symptom’s
The window is merely opened, but it never proceeds, and as a result, this exception is raised.
Solution
Add this line of code while initializing your driver and mostly with Chrome driver.
//chromeOptions.addArguments("--remote-allow-origins=*");
Note: You will observe this issue only with Selenium 4.X only.
Error 2 | org.openqa.selenium.SessionNotCreatedException
In certain instances, you may encounter the following error during Chrome driver initialization.
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500.
Solution
Incorporate the following line of code, and it will resolve your issue.
WebDriverManager.chromedriver().browserVersion("117.0.5938.92").setup();
To confirm the Chrome version that is on your system, Open the browser, go to setting, and About Chrome, Here is the version, note it down.
Note: In some cases updates are installed automatically, and relaunch of the browser is mandatory, so relaunch in that case to see the accurate version of the Chrome browser.
Code
Following is the code where these issues addressed, The Code Open the Chrome browser with the help of driver.
package com.yousafzai.tests;
import com.yousafzai.pageobjects.LandingPage;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import java.io.IOException;
import java.time.Duration;
public class LandingPageTests {
WebDriver driver;
@BeforeClass
public void setUp() {
try {
WebDriverManager.chromedriver().browserVersion("117.0.5938.92").setup();
//WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
// Configure implicit wait for 10 seconds
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Maximize the browser window
driver.manage().window().maximize();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testLogin() throws IOException {
driver.get("https://URL");
}
@AfterClass
public void tearDown() {
// Close the browser
driver.quit();
}
}
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yousafzai</groupId>
<artifactId>Assignment1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Assignment1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.12.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.5.3</version>
</dependency>
</dependencies>
</project>
Alternative Solutions
Root Directory Placement
- Place the ChromeDriver executable in your project’s root directory.
- Modify your test script to set the ChromeDriver path with
System.setProperty
.
In some case you might need to set the path with the help of below command.
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
System Property Configuration:
- Set the ChromeDriver path externally using
System.setProperty
. - Specify the path to the ChromeDriver executable in your test script.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
These alternative methods offer flexibility for configuring ChromeDriver path settings to match your project’s requirements.
Other Tips and Considerations
Following are some good habits one mush consider while dealing with Test automation.
Keep Chrome and ChromeDriver Updated
Regularly update your Chrome browser and ChromeDriver to ensure compatibility and access to the latest features and bug fixes.
Check for Automatic Updates
Be aware that Chrome updates can occur automatically. After updates, remember to check for a new ChromeDriver version that matches your browser.
Use Explicit Waits When Necessary
While implicit waits are useful, consider using explicit waits for specific elements or actions when dealing with dynamic web pages to enhance test stability.
Logging and Error Handling
Implement robust logging and error-handling mechanisms in your test scripts to capture detailed information about failures and debugging.
Conclusion | Connectionfailedexception selenium
“Two commonly occurring issues are ‘org.openqa.selenium.remote.http.ConnectionFailedException’ and ‘The window is opened but never proceeds, leading to this exception.’ This blog provides potential solutions for both problems. In most cases, these issues arise with the latest Chrome build and Selenium version 4. Please share the blog with others experiencing the same problems.”