How to Resolve NoSuchSessionException in selenium?
NoSuchSessionException in Selenium is a tricky issue that requires several techniques to check and follow. In this post, I tried to explain several techniques of how I have resolved one of my cases.NoSuchSessionExpection occurs when we try to perform actions after quitting the session. The classic exception will also guide you further.
The Class Structure of NoSuchSessionException in selenium is as follows:
The details are given here.
public class public class org.openqa.selenium.NoSuchSessionException extends
org.openqa.selenium.WebDriverException
{
//field Information:
public BASE_SUPPORT_URL;
public DRIVER_INFO;
public SESSION_ID;
//constructors:
NoSuchSessionException()
NoSuchSessionException(java.lang.String reason)
NoSuchSessionException(java.lang.String reason,java.lang.Throwable cause)
}
Possible Cause and Solve:
Step-1: Is the Driver Incompatible with Browser?
It is very much possible that our ChromeDriver/ FireFoxDriver/ IEDriver are not compatible with 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 the latest versions should resolve our issue. NoSuchSessionException in selenium can occur due to this.
-> Download chrome driver from – https://chromedriver.chromium.org/downloads
-> Download firefox driver from – https://github.com/mozilla/geckodriver/releases
-> Download IE driver from – https://www.seleniumhq.org/download/
Step-2: What are we using driver.close() or driver.quit()?
Are we using driver.close()? Check if there are some steps left after the driver is closed. If so, driver.close() closes the browser and as there is no active browser, the driver quits. If we code haphazardly, this issue may appear. We need to define a teardown method after the test to call driver.quit().
This issue may also arise when we try to run parallel execution on the 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 teardown method there.
We need to look for driver.close() or driver.quit() method of our code.
We are calling driver.close(). This will close the present window. On the off chance that no open windows remain, the driver stops. Thus, when our tearDown calls the driver.quit() the meeting has finished and you get a mistake.
Arrangements you could utilize:
try not to call driver.close() in our test and let the teardown handle it
or on the other hand
in the teardown, check for a legitimate session and just call driver.quit() in the event that one exists. NoSuchSessionException in selenium can occur due to this.
Step-3: Is there a Tabbing issue?
This issue can arise when we try to open a 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 a new Session. We need to make sure we are not opening multiple browsers during testing. NoSuchSessionException in selenium can occur due to this.
Step-4: is the Browser not ready for doing operations?
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 wherever we are trying to open a browser. NoSuchSessionException in selenium can occur due to this.
Step-5: Is Docker YML configured properly?
If we are using docker, we need to configure the docker-compose.yml file. The default size is 64MM and the recommended size is 256MB. We need to increase it. We need to check for container privilege as well. We need to execute docker with docker run -privileged mode. This flag enables all capabilities for a container. NoSuchSessionException in selenium can occur due to this.
Step-6: is there an Issue in Xpath?
Very surprisingly there could be an exception due to the way we are identifying the element. If there is an 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. NoSuchSessionException in selenium can occur due to this.
Step-7: Is our dependency ready?
Is our dependency file(POM.xml) correct? and is it rightly pointing to the correct artifacts like jars? We need to check this one also while working with Jenkins. NoSuchSessionException in selenium can occur due to this.
Step-8: Are we pointing the correct driver?
Are we pointing the correct driver? It may so happen that instead of pointing to the local driver we are pointing to Remote driver. The driver provides the necessary hooks to work with the browser. So it is extremely important to install and point to the correct drivers. NoSuchSessionException in selenium can occur due to this. We need to use:
Driver driver=new ChromeDriver();
//instead of
Driver driver=new RemoteChromeDriver();
Driver driver=new RemoteWebDriver();
Step-9: Is the proxy configuration correct?
In the case of Katalon, the 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. NoSuchSessionException in selenium can occur due to this.
Step-10: Are we importing the correct package?
Importing incorrect packages may create issues. Say our chrome driver and browser is not compatible in that case if we import upgraded or downgraded package, this issue will occur. NoSuchSessionException in selenium can occur due to this.
The below imports needs our attention:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
Apart from these also check the java version. The java and driver compatibility also plays a crucial role.
- Overhaul JDK to latest levels JDK
- Clean your Project Workspace through your IDE and Rebuild your project with the required conditions as it were.
- On the off chance that your base Web Client adaptation is excessively old, at that point uninstall it and introduce an ongoing GA and discharged rendition of Web Client.
- Take a System Reboot.
- Execute your @Test.
What are the other exceptions that can be resolved with this approach?
The below written exceptions can also be resolved by using this post
- org.openqa.selenium.nosuchsessionexception firefox
- org.openqa.selenium.nosuchsessionexception: invalid session id
- org.openqa.selenium.nosuchsessionexception session id is null
- no such session exception in selenium
- exception in thread main org openqa selenium nosuchsessionexception session id is null
- org.openqa.selenium.nosuchsessionexception: invalid session id in selenium
- org.openqa.selenium.nosuchsessionexception: tried to run command without establishing a connection
- exception in thread “main” org.openqa.selenium.nosuchsessionexception: invalid session id