I am using selenium webdriver with Java.

My test needs to verify when you save your login information and close the browser and reopen it then those credentials remain and are saved on a new session. So I want to close the current session and reopen it in order to verify if a cookie persists on the page however Selenium deletes all stored session data so the test case will always fail. Is there any way to prevent Selenium from deleting the stored session data after closing the browser for the specific test case?

When I do run it I get a no such session error.


3 Answers 3


Well, I believe you cannot prevent deletion of Session data once you close the browser. But, you can store the cookies of your first instance and copy it to new instances using a driver.manage().getCookies() method.

Before calling driver.close() method in your test, make sure to save the cookies using following piece of code :

Set<Cookie> allCookies = driver.manage().getCookies();

The above allCookies variable you can define global as you wish.

So for next instance onward,in the beginning of your test use the below piece of code :

driver = new FirefoxDriver();
for(Cookie cookie : allCookies)

Now this will copy all cookies present earlier to this session, so after this proceed with further logic as per your requirements.

  • This obviously will not work for cookies with HttpOnly flag: stackoverflow.com/questions/15952262/…
    – dzieciou
    Commented Mar 13, 2016 at 12:44
  • 3
    getCookies() returns only cookies for the current domain, while addCookie() allows to add only a cookie with domain same as current URL's domain. So when getting or setting cookies it is important to go to the right URL first.
    – dzieciou
    Commented Mar 13, 2016 at 12:46
  • 1
    Thanks @dzieciou . I was initially trying to do this before any pages loaded at all (About: Blank) and it was causing an exception
    – HRVHackers
    Commented May 14, 2019 at 21:31
  • Any idea on how to perform this in chrome C# (Visual Studio) ? Thanks Commented Mar 14, 2020 at 7:37

Well the session of a web driver instance is over once that particular instance is closed. Thus you can't access the session of the previous instance in a new one. However you can still achieve the session by storing the session value in a variable and then adding the session in the new instance. Look at the below mentioned code for better understanding:

public void useStoredSessionInNewWindow() {
 // initiate web driver and go to an website
 _webDriver = new FirefoxDriver();

 // add code to login in the website

 // store the current session
 Set<Cookie> cookiesInstance1 = _webDriver.manage().getCookies();
 System.out.println("Cookies = "+cookiesInstance1);

 // close the web driver instance

 // again initiate web driver and go to the same website. This will open the login page
 _webDriver = new FirefoxDriver();

 // add the stored session in the bew web driver instance
 for(Cookie cookie : cookiesInstance1)

 // re-visit the page

 // get the current session of new web driver instance
 Set<Cookie> cookiesInstance2 = _webDriver.manage().getCookies();
 System.out.println("Cookies = "+cookiesInstance2);

 // notice that session of previous web driver instanse is achieved
 Assert.assertEquals(cookiesInstance1, cookiesInstance2);


Hope this helps :)


you could try using an implicitly specified profile path - then all data that your browser would have saved in a real-life situation when you close the browser will still be saved.

options.addArguments("--user-data-dir=" + PROFILE_PATH);
  • 1
    implicitly specifying profile path wasn't enough.
    – ufk
    Commented Feb 18, 2020 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.