3

I need to use a shared profile for Firefox, which doesn't get deleted on exit. Seems that this can be done using a FirefoxProfile or FirefoxOptions. But none of them seems to work: When starting the geckodriver, it uses a temp profile like this

1507646897935 mozrunner::runner INFO Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette" "-profile" "C:\Users\\AppData\Local\Temp\rust_mozprofile.uzI9KAmLQ1zP"

When debugging, I noticed that the property ProfileDirectory of the profile is always null.

var profileManager = new FirefoxProfileManager();
var profile = profileManager.GetProfile("Test");
var driver = new FirefoxDriver(profile);

The profile Test was created manually using firefox -pbefore. I also tried to use it's location like this:

var profile = new FirefoxProfile(@"C:\Users\<MyUsername>\TestProfile", deleteSourceOnClean: false);

But same problem, can't figure out why this isn't working.

Used software

  • geckodriver 0.19.0
  • Selenium.Firefox.WebDriver 2.0.0 (NuGet)
  • Selenium.WebDriver 3.6.0 (NuGet)
  • ASP.NET Core 2.0
Lion
  • 16,606
  • 23
  • 86
  • 148

2 Answers2

0

Solved this issue by passing the path to my profile as regular CLI parameter to Chrome:

var options = new ChromeOptions();
options.AddArgument(@"--user-data-dir=C:\Users\<MyUsername>\TestProfile");
var driver = new ChromeDriver(options);

Should also work with Firefox. But I needed to switch to Chrome until another bug in the FF driver got fixed. This is no full clean solution at all, but it works as a workaround until a better solution is found.

Lion
  • 16,606
  • 23
  • 86
  • 148
0

In Firefox, I needed to preserve all the cookies, history, cache etc and nothing worked since selenium isn't built to save any of these across sessions for obvious reasons.

Since there is no solution for Firefox, here is how I hacked it

  1. Read the firefox.exe command line in order to find out whats the profile temp dir.
  2. Manually close the browser so the temp profile isn't deleted
  3. Move the temp profile with all the data preserved

Here is the code:

IWebDriver _driver;
var service = FirefoxDriverService.CreateDefaultService();

//Start webdriver
_driver = new FirefoxDriver(service, options);


//get the webdriver commandline so we can get the path of the ff profile temp dir so we can save it later
var proc = service.ProcessId;
string cmdline = GetCommandLine(proc);
string profilePath = cmdline.Substring(cmdline.IndexOf(" -profile ") + 10);





//Do stuff with your browser




//In order to move the temp profile dir, we have to 'manually' close the browser window.
//There is no other way because the temp profile gets deleted if you use _driver.close()
var ffprocess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle == $"{title} - Mozilla Firefox");
ffprocess.CloseMainWindow();

//Delete the old profile data so we can get the updated data.
Directory.Delete(sProfile, true);

//store the temp profile data
Directory.Move(profilePath, sProfile);
//this must be deleted, othervise the webdriver won't start next time
File.Delete(sProfile + @"\user.js");




string GetCommandLine(int process)
{
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher($"SELECT CommandLine FROM Win32_Process WHERE CommandLine Like \"% -profile %\" AND ParentProcessID = {process}"))
    using (ManagementObjectCollection objects = searcher.Get())
    {
        return objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString();
    }

}
Milos
  • 2,927
  • 1
  • 15
  • 27