NoSuchSessionException in selenium

How to Resolve NoSuchSessionException in selenium?
How to Resolve NoSuchSessionException in selenium?

How to Resolve NoSuchSessionException in selenium?

If we are getting org.openqa.selenium.NoSuchSessionException: No active session with ID during our testing process,It means that the driver we are using is not able to connect or initiate an active session with underlying browser. We will see series of steps for different scenarios to overcome the issue.
Now let us check how this Exception class looks like:

The class Structure of NoSuchSessionException is as follows:

public class public class org.openqa.selenium.NoSuchSessionException extends 
//field Information:
public SESSION_ID;
NoSuchSessionException(java.lang.String reason) 
NoSuchSessionException(java.lang.String reason,java.lang.Throwable cause) 

Possible Cause and Solve:


It is very much possible that our ChromeDriver/ FireFoxDriver/ IEDriver are not compatible the Chrome, FireFox,IE browser, we are using along with Selenium Client.We need to make sure if they are correct. We also need to check for JDK version compatibility.We need to downgrade or upgrade the versions to check for a solution.Ideally all latest versions should resolve our issue.


Are we using driver.close()? Check if there are some steps left after driver is closed.If so, driver.close() closes the browser and as there is no active browser, the driver quits. If we code hapazardly, this issue may appear. We need to define a teardown method after the test to call driver.quit().
This issue may also arises when we try to run parallel execution on selenium grid. Just in case we are calling driver.quit() before any other test,the driver is set to null and the subsequent test does not get the proper driver information to work on it.
We need to implement @AfterTest or @AfterGroup annotation and define the tear down method there.
We need to look for driver.close() or driver.quit() method of our code.


This issue can arises,when we try to open new Window instead of Tab under a driver. Make sure the Tab is opened under the driver not a new Window. New Window may need new Session.We need make sure we are not opening multiple browsers during testing.


This issue can also appear if we have started testing i.e @Test before even browser launches properly. If the test starts running before a successful handshake between browser and driver,the session info is invalid.We need to give enough sync time between browser launch in the @before annotation or whereever we are trying to open a browser.


If we are using docker,we need to configure the docker-compose.yml file.The bydefault size is 64MM and the recommanded size is 256MB. We need to increase it.We need to check for conatainer priviledge as well. We need to execute docker with docker run –priviledged mode.This flag enables all capabilities for a container.


Very surprisingly there could be an exception due to the way we are identifying the element. If there is any error in Xpath,id or CSS or the way,we are identifying the element , this exception can occur. We need to double check the element finding strategy.


Is our dependency file(POM.xml) correct? and is it righly pointing to the correct artifacts like jars.We need to check this one also while working with Jenkins.


Are we pointing correct driver? It may so happen that instead of pointing to the local driver we are pointing to Remote driver
We need to use:

Driver driver=new ChromeDriver();
//instead of 
Driver driver=new RemoteChromeDriver();
Driver driver=new RemoteWebDriver();


In case of Katalon, incorrect proxy configuration can break the code and throw this exception.The default proxy option for Katalon studio is No Proxy.Either we need to change the proxy to our desired one by navigating Run->Katalon Studio Preferences->Katalon->Proxy and change to your desired one. or we can select Use System Configuration. This will enable the browser and driver to have a proper handshake.