[Solved] org.openqa.selenium.SessionNotCreatedException – Could not start a new session. Response code 500 – Java

by
Ali Hasan
google-chrome selenium-webdriver

Quick Fix: Using Selenium v4.11.1, avoid manual chromedriver.exe path configuration. Delete the old driver (v96) from usr/local/bin/chromedriver. Let Selenium download the latest driver programmatically.

The Problem:

When attempting to initialize a ChromeDriver instance for web automation using Selenium, an exception is encountered – ‘org.openqa.selenium.SessionNotCreatedException’. The exception message indicates that a new session could not be created with a response code of 500. The underlying cause seems to be a version mismatch between the installed ChromeDriver and the version of Google Chrome installed on the system.

The Solutions:

Solution 1: SeleniumManager

SeleniumManager is a built-in tool available in Selenium 4.11.1. It handles driver management automatically. You don’t need to set the `chromedriver.exe` path manually using `System.setProperty` or use third-party tools like `WebDriverManager`.

However, in your updated error message, there seems to be an issue with the version of the chromedriver. Selenium should have downloaded and used the latest version matching your Chrome browser (v115), but somehow it is still using the v96 driver.

To resolve this issue, follow this solution.

  1. Delete the old driver (v96) from the cache. You can find it at this path:
    usr/local/bin/chromedriver
    
  2. Allow Selenium to download the latest driver programmatically.

This should fix the problem.

Solution 2: Check for conflicting ChromeDriver versions and update accordingly

The error message clearly indicates a version compatibility issue between the ChromeDriver and Google Chrome. A new warning message has been added to notify users about this conflict and advise them to delete conflicting driver versions from their path.

To resolve this issue, follow these steps:

  1. Check your system path for multiple ChromeDriver versions.
  2. Delete the conflicting ChromeDriver versions that are not compatible with your current Chrome version.
  3. Restart your computer to ensure the changes take effect.
  4. Run your Selenium code again.

Alternatively, you can also set the webdriver.chrome.driver system property to specify the location of the ChromeDriver executable explicitly. Make sure to use a compatible version of ChromeDriver for your Chrome version.

Here’s an example of setting the webdriver.chrome.driver property in Java:

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

Once you have updated the ChromeDriver version or set the webdriver.chrome.driver property correctly, the error should be resolved, and your Selenium code should run without issues.

Solution 3: Use Selenium Manager

If you are using Selenium version 4.11.0, you can remove the System.setProperty line of code. Delete the ChromeDriver version 96.0.4664.45 from the path /usr/local/bin/. This allows Selenium Manager to download the matching ChromeDriver silently and execute the test.

Your code should look like this:

WebDriver driver = new ChromeDriver();
   driver.manage().window().maximize();
   driver.manage().deleteAllCookies();
   driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
   driver.get("https://www.google.com/");

Q&A

What is the reason behind this error and how to modify the code?

Use SeleniumManager and delete the old chromedriver.exe manually.

What does the new warning message indicate?

Conflicting driver versions in the path.

What is the significance of the error message and how to resolve it?

Remove System.setProperties & use SeleniumManager.

Video Explanation:

The following video, titled "Related Video", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Error : Could not start a new session. Response code 500 When launching browser in Selenium Webdriver Debug : 1.