Best Way to Preserve Cookies for the Long Term from One Selenium Session to Another?

Viewed 548

I'm using Selenium WebDriver to launch a specific Firefox Profile. Normally, Selenium creates a temporary installation of FF that mimics an existing Firefox Profile which is then deleted after calling webdriver.close().

However, I need to be able to launch Firefox WebDriver via Selenium and preserve certain login cookies for future session so that I don't get asked to enter a password and/or verification code every time I login to an account. I have seen a few ways to tackle this problem but each has its own problems:

1) Identify the temporary files created by Selenium and copy cookies.sqlite to the original FF Profile installation folder. The problem I see with this approach is that I'm launching many instances of Selenium at the same time so how would I identify which temporary folder corresponds to a specific WebDriver session? Also, would it be possible to append cookies to original installation file instead of overwriting the original file?

2) One user suggested calling:

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

before calling driver.close() and then add cookies to next WebDriver session by calling:

for(Cookie cookie : allCookies)
{
    driver.manage().addCookie(cookie);
}

The problem I see with this approach is that I need cookies to be preserved for the long term (i.e, forever) and I cannot store cookies of all sessions in memory. Even if I could, cookie data would be lost when the program exists or the host machine is turned off. Additionally, 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 as pointed out by one commenter. I want to load cookies from multiple domains immediately when the new FF session is launched.

3) Another option might be to prevent Firefox from creating temporary files in new location in the first place (i.e, redirect to original FF installation directory) and prevent FF from deleting the temporary files upon exit. Is something like this viable?

I've seen some other approaches but they either only address this question as it relates to Chrome (not Firefox) or in Python or they suffer from one of the flaws mentioned above.

What approach can be used to solve this problem in Java + Firefox?

Thanks!

1 Answers

The best solution for you is when you want to store them to store your cookies in a JSON file, so you'll be able to load them in your browser later.

Here is a quick tutorial how to store them: TUTORIAL

Write in JSON:

import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class WriteJSONExample
{
  @SuppressWarnings("unchecked")
  public static void main( String[] args )
{
    //First Employee
    JSONObject employeeDetails = new JSONObject();
    employeeDetails.put("firstName", "Lokesh");
    employeeDetails.put("lastName", "Gupta");
    employeeDetails.put("website", "howtodoinjava.com");
     
    JSONObject employeeObject = new JSONObject(); 
    employeeObject.put("employee", employeeDetails);
     
    //Second Employee
    JSONObject employeeDetails2 = new JSONObject();
    employeeDetails2.put("firstName", "Brian");
    employeeDetails2.put("lastName", "Schultz");
    employeeDetails2.put("website", "example.com");
     
    JSONObject employeeObject2 = new JSONObject(); 
    employeeObject2.put("employee", employeeDetails2);
     
    //Add employees to list
    JSONArray employeeList = new JSONArray();
    employeeList.add(employeeObject);
    employeeList.add(employeeObject2);
     
    //Write JSON file
    try (FileWriter file = new FileWriter("employees.json")) {
        //We can write any JSONArray or JSONObject instance to the file
        file.write(employeeList.toJSONString()); 
        file.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Read in JSON:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
 
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 
public class ReadJSONExample 
{
    @SuppressWarnings("unchecked")
    public static void main(String[] args) 
    {
        //JSON parser object to parse read file
        JSONParser jsonParser = new JSONParser();
         
        try (FileReader reader = new FileReader("employees.json"))
        {
            //Read JSON file
            Object obj = jsonParser.parse(reader);
 
            JSONArray employeeList = (JSONArray) obj;
            System.out.println(employeeList);
             
            //Iterate over employee array
            employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
 
    private static void parseEmployeeObject(JSONObject employee) 
    {
        //Get employee object within list
        JSONObject employeeObject = (JSONObject) employee.get("employee");
         
        //Get employee first name
        String firstName = (String) employeeObject.get("firstName");    
        System.out.println(firstName);
         
        //Get employee last name
        String lastName = (String) employeeObject.get("lastName");  
        System.out.println(lastName);
         
        //Get employee website name
        String website = (String) employeeObject.get("website");    
        System.out.println(website);
    }
}
Related