Selenium has revolutionized web automation by empowering testers to streamline their workflows and improve test coverage. However, like any powerful tool, Selenium can sometimes throw curveballs in the form of unexpected exceptions.
In this blog, we will explore a common Selenium error: org.openqa.selenium.SessionNotCreatedException
, which means WebDriver can’t create a new session for a browser.
Whether you’re a seasoned automation engineer or just starting your Selenium journey, you’ll gain valuable insights to troubleshoot effectively and write more robust test scripts in this blog.
What is ‘org.openqa.selenium.SessionNotCreatedException’?
A session is basically a “connection” between the test script and the browser so it can interact with it. The SessionNotCreatedException is Selenium’s way of telling you that it could not create a connection with the browser. If this happens, then your Selenium scripts will not run on the web browser.
Reasons for SessionNotCreatedException
Let’s take a look at the main reasons why this exception occurs:
Browser and WebDriver Version Mismatch
Every browser has a specific version. Each version of WebDriver (like ChromeDriver for Chrome or GeckoDriver for Firefox) is designed to work with specific browser versions. If the browser is updated, but the WebDriver is not, the WebDriver might not be able to communicate properly with the browser because of differences in how the browser behaves.
Incorrect Driver Path
When you try to run your test, Selenium needs to know where the WebDriver executable (like chromedriver.exe for Chrome) is located. If it can’t find the driver, it won’t be able to start a session.
Incompatible Browser Features or Settings
Sometimes, specific browser settings or features can prevent the WebDriver from creating a session. This might happen if features like sandboxing, permissions, or security settings interfere with WebDriver’s ability to launch the browser.
WebDriver Version Outdated
If you’re using an older version of WebDriver, it might not support newer features or browser updates. This can lead to compatibility issues.
Browser Not Installed or Corrupted
If the browser you’re trying to automate isn’t installed or its installation is corrupted, the WebDriver won’t launch it. This will result in a session creation failure.
Multiple WebDriver Instances Running
At a given time, there could be multiple WebDriver instances running. There could also be conflicts with other automation tools. This can prevent a new session from being created.
Browser is Running in Headless Mode (or Similar Mode)
Some browsers, like Chrome, can run in “headless” mode, which means without a graphical user interface (GUI). If the configuration is wrong, it may cause the WebDriver to fail when trying to create a session.
How to Handle Session Not Created Exception in Selenium
You can use the following approaches to handle this exception.
Ensure Browser and WebDriver Compatibility
Always ensure that the browser version and the corresponding WebDriver are compatible. You can use WebDriverManager to automatically manage the correct WebDriver version for your browser.
WebDriverManager.chromedriver().setup();
automatically downloads and configures the correct version of the ChromeDriver based on your browser version.public class SeleniumExample { public static void main(String[] args) { // Automatically download the correct version of ChromeDriver WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.example.com"); driver.quit(); } }
Check and Correct WebDriver Path
If the WebDriver executable is not in the right location or the path is incorrect, the session will not be created. Ensure that the WebDriver is installed in the correct location or explicitly specify its path.
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver")
tells Selenium where to find the ChromeDriver executable. Make sure it is correct.System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://www.example.com"); driver.quit();
Ensure the Browser Is Installed Properly
If the browser is not installed properly or is corrupted, Selenium cannot launch it. Reinstall the browser and ensure that it is correctly installed. When you write the test case, ensure that the ChromeDriver is pointing to the correctly installed version of Chrome.
Check Permissions
The session will not be created if you do not have the required permissions to execute the WebDriver or launch the browser. Make sure that the WebDriver has the correct permissions to run.
For example, the bash command chmod +x chromedriver
will check if the chromedriver file is executable.
Handle Browser-Specific Options or Settings
options.addArguments("--headless")
makes Chrome run without a UI, which can help in some cases where there are display or environment issues.public class SeleniumExample { public static void main(String[] args) { ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); // Run Chrome without UI WebDriver driver = new ChromeDriver(options); driver.get("https://www.example.com"); driver.quit(); } }
Close Other WebDriver Sessions
If multiple WebDriver sessions are running or there are leftover sessions, it can cause conflicts and prevent new sessions from being created. Ensure no other WebDriver instances are running, and you can clean up previous sessions.
You can manually terminate any hanging WebDriver processes through the command prompt.
taskkill /IM chromedriver.exe /F
// On windowspkill chromedriver
// On Linux/MacOS
Wait for the Browser to Fully Initialize
Sometimes, the WebDriver may try to start a session before the browser is fully ready, leading to errors. Use explicit waits to ensure the browser is fully ready before starting a session.
Use Remote WebDriver for Cloud-Based Testing
If you’re running the test on a local machine with limited resources, it might cause issues in creating sessions. Use a cloud-based infrastructure for remote testing to avoid session creation issues.
Use WebDriver Logs for Debugging
Sometimes, the error might not be clear, and you need more information about why the session failed to be created. Enable WebDriver logs to capture detailed information about the error. You can enable logging with options.setCapability(“loggingPrefs”, “ALL”) to capture logs that could indicate why the session creation failed.
Conclusion
While exceptions can be frustrating, they often help us learn more about the way we write test scripts. By understanding what the exception is trying to tell us and taking corrective measures, we can create reliable test scripts that run without giving flakey results.