Why Won't Firefox WebDriver Load Cookies from Previous Browser Session?

Viewed 582

I'm launching a specific FF Profile via Selenium and I'm trying to preserve cookies from one session to the next (i.e, cookies should be preserved even after calling driver.quit())

I'm aware, WebDriver normally generates a temporary folder mimicking the original profile so when the WebDriver instance is terminated any cookies associated with the current (temp) instance is deleted. However, in my program I copied cookies.sqlite from the temporary folder to the original folder (overwriting the cookies original file). I was expecting FF to launch next time with the new cookies from previous session but it turns out that, for some odd reason, the cookies are still not present.

I checked all around and it seems cookies are in fact stored in cookies.sqlite.

If so why isn't Firefox / WebDriver recognizing the cookies that I copied from the previous (temp) session? Are there any other files I need to copy as well?

I'm using Java.

Thanks

1 Answers

Try this code.

FirefoxOptions options = new FirefoxOptions();
options.addArguments("user-data-dir=C:.\cookies"); //or your absolute path
driver = new FirefoxDriver(options);

What this will do is, check for "cookies" folder or whatever name you give at the specified location. If the folder doesn't exist e.g. 1st execution of the Program, Selenium will create folder at that location automatically and will save the cookies and infromation from your Browser. Next time when you will run your program, it will use those cookies. This way, your cookies will remain even after days.

Here is my C# code for Chrome Browser which worked perfectly. You will login to website and next time you run the program, you will not have to login again since your cookies are saving in the folder.

ChromeOptions options = new ChromeOptions();
options.AddArgument(@"user-data-dir=.\profiles");
ChromeDriver driver = new ChromeDriver(options);
Related