0

On doing a driver.close();driver.quit(); during the execution of java code, the following error is thrown:

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Tried to run command without establishing a connection Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z' System info: host: 'ADMIN-PC', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_151' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{moz:profile=C:\Users\admin\AppData\Local\Temp\rust_mozprofile.ENTBvl2aDbSs, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, platform=XP, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=56.0.2, platformVersion=10.0, moz:processID=5004, browserName=firefox, javascriptEnabled=true, platformName=XP}] Session ID: 82e7dabd-c178-4d90-a3f8-84dc3f6ff14f at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:643) at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:482) at yahoo.main(yahoo.java:34)

Sharing code that throws the above exception :

//package basicSeleniumScripts;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class yahoo {

public static void main(String[] args) {
    String Firefoxdriverpath = "C:\\Marionette\\geckodriver_1.exe";
    WebDriver driver;

    System.setProperty("webdriver.gecko.driver",Firefoxdriverpath);
    //create a new instance of Firefox driver
    driver = new FirefoxDriver();
    //Open the page we want to open
    driver.get("http://www.yahoo.com");
    //Defining expected title 
    String expectedTitle = "Yahoo";
    //Getting the actual title
    String actualTitle = null;
    actualTitle = driver.getTitle();

    //Validating the TestCase

    if (actualTitle.contentEquals(expectedTitle))
    {
        System.out.println("Test Passed");
    }

    else 
        {
        System.out.println("Test Failed!!!");
        }
    driver.close();
    driver.quit();
}


}
mrfreester
  • 1,981
  • 2
  • 17
  • 36
Sudeept
  • 123
  • 2
  • 10
  • Please share your code – iamsankalp89 Nov 08 '17 at 16:39
  • Please find the code in above edit – Sudeept Nov 08 '17 at 16:45
  • What happens if you just call `driver.quit()` without calling `driver.close()`? [You should only need to call quit.](https://stackoverflow.com/questions/15067107/difference-between-webdriver-dispose-close-and-quit) That being said, I wouldn't expect this error. – mrfreester Nov 08 '17 at 16:49
  • Which version of Firefox and gecko driver you used.. also try your code by removing close and quit statement – iamsankalp89 Nov 08 '17 at 16:49
  • Don't forget dispose, after your close! – JOberloh Nov 08 '17 at 17:17
  • 1
    @JOberloh `dispose` doesn't appear to be in the java bindings anymore. They probably removed it since `quit` disposes it anyway. – mrfreester Nov 08 '17 at 17:50
  • if i use driver.quit() only or driver.close() only, the code runs without throwing any exception – Sudeept Nov 08 '17 at 18:35
  • Looking at the api, this might make sense. It's hard to tell exactly but the wording makes it sound like they made a change so if `driver.close` is called and it's the last window open, it might close the browser so that `driver` is no longer attached to the session when `quit` is called. – mrfreester Nov 08 '17 at 18:41

1 Answers1

2

Update the gecko driver version to v0.19.0 as you are using 3.6.0 jars of selenium.

Also use quit method only

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
  • `close` only closes the current window, but the driver will still be running unless `quit` is called. In most cases, `quit` is all that needs to be called so you don't have a bunch of zombie driver instances continuing to run. – mrfreester Nov 08 '17 at 17:07