2

I'm new to Java and Selenium and have this issue where I need to reuse the browser session.

I've searched around, but could not find a good solution for that. Is there a way to reuse Firefox session in Selenium with Java?

Turbut Alin
  • 2,568
  • 1
  • 21
  • 30
Brito
  • 67
  • 1
  • 14

1 Answers1

1

You have two options:

  1. Save your cookies and retrieve them at each creation of the driver

    driver = new FirefoxDriver();
    for(Cookie cookie : allCookies)
    {
        driver.manage().addCookie(cookie);
    }
    
  2. Save your browser profile locally and then load it

    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    File profileDirectory = new File("c://mach//lib//prof");
    FirefoxProfile profile = new FirefoxProfile(profileDirectory);
    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    WebDriver driver = new FirefoxDriver(capabilities);
    
Turbut Alin
  • 2,568
  • 1
  • 21
  • 30
  • Tx for the help but, how about saving firefox profile or saving cookies? I'm not able to achieve this. – Brito Nov 29 '16 at 12:09
  • Have you tried saving the cookies as a set: `Set cookies = driver.manage().getCookies();` and then retrieving them? Firefox saves profiles into a tmp folder which then gets deleted at shutdown, but you can still go into this tmp folder and save the profile in a file before. Reference here: http://stackoverflow.com/questions/30435749/python-selenium-firefox-cant-start-firefox-with-specified-profile-path/33350778#33350778. Alternatively, you can do this: http://stackoverflow.com/questions/13033071/save-firefox-profile-generated-by-selenuim-web-driver – Turbut Alin Nov 29 '16 at 12:17
  • when I try the code above for the cookies, I get ther error "allCookies cannot be resolved to a variable" any idea? – Brito Nov 29 '16 at 12:39
  • This error is becaouse I put the 'Set cookies = driver.manage().getCookies();' line when I want it to save the cookies... I thought that it should be inserted right befor the 'driver.quit' so, all cookies wold be saved befor closing the browser. – Brito Nov 29 '16 at 12:44
  • change the variable name from allCookie to cookies or viceversa (-: – Turbut Alin Nov 29 '16 at 13:42