SessionNotCreatedException in Selenium
By Sonal Dwivedi, Community Contributor - December 13, 2024
Selenium is the most popular and widely used automation tool by web automation testers. At the initial stages of trying out this tool and setting up the web automation suite, testers may face different Selenium Exceptions.
Out of these, SessionNotCreatedException is a common exception that is experienced mostly by every tester.
This guide explores SessionNotCreatedException, why it occurs, solutions to overcome the exception, and why you must execute Selenium Tests on real devices.
- What is SessionNotCreatedException?
- Common Causes of SessionNotCreatedException
- Solutions for SessionNotCreatedException in Selenium
- How to Avoid SessionNotCreatedException in Selenium?
- 1. Use WebDriverManager
- 2. Regularly updating the WebDriver and the Browser Binaries
- 3. Use Browser Automation Platforms
What is SessionNotCreatedException?
SessionNotCreatedException occurs when there is a mismatch or compatibility issue between the browser and WebDriver versions. When Selenium cannot start a new browser session, SessionNotCreatedException is thrown.
For example, if the browser on your system is version 130.0 and you wrongly downloaded the Chromedriver 131.0 version from the Chromedriver version selection page and try to launch Chrome browser via Selenium script, you will get the SessionNotCreatedException.
Read More: Exception Handling in Selenium WebDriver
Common Causes of SessionNotCreatedException
The SessionNotCreatedException exception in Selenium is commonly caused by version mismatches, outdated drivers, or incorrect WebDriver configurations.
1. Browser and WebDriver Versions Mismatch: This is one of the most common reasons for SessionNotCreatedException in which the version of the browser you are trying to automate is not compatible with the corresponding WebDriver version.
For example, The Chrome browser version on the system is 130, and the ChromeDriver version is 131.
2. Outdated WebDriver Binaries: If you are using an outdated WebDriver version which is not supporting the current browser version, you may face SessionNotCreatedException. Try updating the WebDriver version to support the browser version which you are trying to automate.
3. Permission Issue with WebDriver: At some instances, it may happen that the WebDriver may not have the necessary permission to access the browser due to insufficient access right to the folder in which WebDriver resides.
4. Corrupt WebDriver or browser executable: If the WebDriver or the browser was not downloaded successfully due to poor network bandwidth or fluctuation, Selenium may throw SessionNotCreatedException.
5. Unsupported Browser Features: When attempting to use a browser feature that is not supported by an older browser version, Selenium may throw SessionNotCreatedException.
Solutions for SessionNotCreatedException in Selenium
Resolving the SessionNotCreatedExcception exception in Selenium involves updating drivers, ensuring version compatibility, and configuring WebDriver correctly.
1. Check Browser and WebDriver compatibility: When there is a mismatch between browser and WebDriver version compatibility, verify the version of the installed browser version and WebDriver to understand where the issue lies.
For example, to check the installed Chrome browser version, go to Chrome’s settings -> Help and check the version. Similarly, to check Firefox’s browser version, go to Firefox’s Help-> About Firefox.
2. Update the WebDriver: There may be instances when the already downloaded WebDriver version becomes outdated, especially when the browser is automatically updated on the system. Make sure you are using the latest WebDriver version that is compatible with the updated browser.
3. Provide Permission to the WebDriver or Browser Executables: SessionNotCreatedExcception may also arise when the WebDriver or the browser does not have sufficient permissions to execute. Ensure that they have the required read/write/execute permission before starting with the Selenium execution.
4. Re-download the WebDriver or Browser Executables: If your browser or WebDriver executable is corrupt, try reinstalling it to fix the SessionNotCreatedException. Then, manually launch the browser from the specified location to verify that it is installed and functioning properly.
5. Use WebDriverManager: With the release of Selenium 4, Selenium Manager also got released which takes care of automated browser and driver management.
If you are working with Maven projects, simply add Bonigarcia’s WebDriverManger Maven dependency from Maven repository in the pom.xml file and instead of
System.setProperty("webdriver.chrome.driver", "/absolute/path/to/binary/chromedriver"); You may just use, WebDriverManager.chromedriver().setup();
And this will automatically check for the latest version of the specified WebDriver binary and if the binary is not present on the system, it will download it.
The simplified code would be as follows:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class SessionNotCreated { public static void main(String args[]) { WebDriverManager.chromedriver().setup(); WebDriver driver=new ChromeDriver(); driver.get("https://www.google.com"); driver.close(); } }
How to Avoid SessionNotCreatedException in Selenium?
Avoiding the SessionNotCreatedException exception in Selenium requires proper configuration, version compatibility, and best practices in WebDriver setup.
1. Use WebDriverManager
Using BoniGarcia’s WebDriverManager would eliminate the SessionNotCreatedException as it automatically downloads and manages the appropriate WebDriver for the browser, thus preventing mismatch issues.
2. Regularly updating the WebDriver and the Browser Binaries
The main reason for SessionNotCreatedException is a version mismatch between WebDriver and browser, which can be avoided by regularly updating the WebDriver and browser binaries.
3. Use Browser Automation Platforms
Downloading, configuring, and setting up the Selenium jars and related dependencies are, at times, confusing, time-consuming, and prone to errors. Investing in a good browser automation platform such as BrowserStack would benefit the tester by providing a readymade platform that has pre-configured environments to handle browser and WebDriver compatibility issues
Why execute Selenium Tests on Real Devices?
Different devices and browsers render web pages differently. To ensure that a given application behaves uniformly across different pools of devices and browsers, it is advisable to test it in real devices and browsers with a real environment set up.
In today’s mobile market, it is not feasible to procure and test on all the latest devices and browsers. To cope with this issue, it is best to invest in any platform that aids in testing the application on a wide range of real devices and browsers.
BrowserStack’s real device cloud provides a comprehensive infrastructure for cross-device and cross-browser testing with zero hassle in setting up the device and environment.
Testing on real devices and browsers ensures a high-quality user experience and compatibility across different platforms, and it validates the application’s performance and security in real time conditions.
How to run Selenium Tests on BrowserStack?
Here are the steps to run Selenium Tests on Real Chrome Browsers with BrowserStack Automate using BrowserStack SDK:
Step 1. Download BrowserStack Project Zip from the GitHub page.
Step 2. Once it is downloaded, unzip it in a desired location in your local system.
Step 3. Import the project in Eclipse via File-> Import -> General -> Projects from Folder or Archive.
Step 4. Once the project is imported it should have a structure like below. Open browser.yml file which contains all the required capabilities to run the tests on BrowserStack.
Step 5. Set username and password in the browserstack.yml file available at the root directory.
Step 6. You can run the test cases on multiple devices and browser combinations available in the BrowserStack cloud. To do so, select the required combinations from the selection list which is available on this page.
Step 7. Copy and replace the platforms object in the browserstack.yml file like below.
- os: Windows osVersion: 10 browserName: Chrome browserVersion: latest - deviceName: iPhone 13 osVersion: 15 browserName: Chromium deviceOrientation: portrait
This is for running the test cases on 2 combinations. If you wish to run only on a single device, you may edit the platforms accordingly.
Step 8. Install the BrowserStack Plugin – on the Eclipse toolbar by navigating to Help > Eclipse Marketplace. In the Eclipse Marketplace, search for BrowserStack > click Install > Finish.
Step 9. Add your desired Selenium test under the src/test/java folder and com.browserstack package. This class should extend the Selenium Test as it has the setup and teardown methods.
Step 10. Run the program and view the test result on the Automate dashboard.
Conclusion
To resolve a SessionNotCreatedException, you need to address compatibility issues between the browser and the WebDriver. This can be done by updating or re-downloading the correct WebDriver or browser binaries. Additionally, if you want to run Selenium tests smoothly across multiple devices and in parallel, it’s essential to manage the necessary infrastructure. BrowserStack provides a comprehensive solution for this, enabling seamless cross-device testing.
BrowserStack Automate provides access to 3500+ devices, browsers, and operating system combinations to test your applications across different environments without needing to maintain complex infrastructure. It helps to accomplish cross-device, cross-platform, cross-browser, and parallel testing