5

Why Selenium always create temporary Firefox Profiles using Web Driver though I told it to use a existing one ?

According to this answer it is not possible to stop Selenium from creating temporary Firefox Profiles using Web Driver. But with chromedriver I can achieve this. So why it is different for Firefox. I checked the FirefoxProfile.cs of Selenium repo and found the following code snipet is used to copy profile---

public void WriteToDisk()
    {
        this.profileDir = GenerateProfileDirectoryName();
        if (!string.IsNullOrEmpty(this.sourceProfileDir))
        {
            FileUtilities.CopyDirectory(this.sourceProfileDir, this.profileDir);
        }
        else
        {
            Directory.CreateDirectory(this.profileDir);
        }

        this.InstallExtensions();
        this.DeleteLockFiles();
        this.DeleteExtensionsCache();
        this.UpdateUserPreferences();
    }

But for chorme there is no such things.

Is it because webdriver install an extension (webdriver.xpi) to communicate with firefox whereas chromedriver.exe is used to interact with chrome.

If that is the reason, in version 3.0 webdriver is using geckodriver.exe to communicate with firefox. So after version 3.0 webdriver will create temporary profile for firefox any more ?

Update: Today I played with webdriver v 3.0+ and found that latest version with legacymode off is still generating temporary profile named as rust_mozprofile.wUqPXh48avDR. My assumption is this temporary profile is generated by geckodriver.exe which is written in Rust

I have used chromedriver 3 years back and not sure chromedriver.exe also generates such type of temporary file. Expecting answer from experts...

Community
  • 1
  • 1
Rasel
  • 820
  • 7
  • 12

2 Answers2

2

The main reason that the Firefox driver uses a temporary profile is to support the use case of running multiple independent simultaneous instances of Firefox. At one time, when Firefox launched, it would drop a sentinel or lock file in the profile directory, and would detect that file if the user attempted to start a new instance of Firefox, preventing them from doing so. Whether or not Firefox still exhibits this behavior, the driver still has to work with some older versions of the browser, and has to account for it. The Selenium project's solution to that issue with WebDriver, when a user wants to use a specific profile, is to copy the contents of that profile to a new directory, and launch Firefox pointing to the copy.

It sounds like Mozilla's implementation does largely the same thing. I would guess it's for the same reason - to support the multiple-instance use case.

JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • So theoretically, if I had 10 pre-made profiles then I can use those simultaneously for launching 10 browsers without copying them, right ? – Rasel Nov 13 '16 at 07:23
  • 2
    It would be nice if selenium would delete those profiles from tmp when finished. – 111 Jun 28 '17 at 22:18
  • It does delete the temp folders if `quit()` is used -- see https://stackoverflow.com/a/30447335/5267751 – user202729 Jan 02 '21 at 03:20
2

I don't how you're handling it since 2017 guys, but I spent some time on it too and I found a convenient solution (Python 3.8 and W10).

I first tried to load a profile in Firefox I previously created "naturally", I mean without Selenium. I configured a proxy on it, and I had cookies recorded (account logged on a website).

I failed many times to load it with Selenium despite all the goods methods described here. Actually I never managed to load it.

So I let Selenium create a temporary profile, configured my proxy on it and logged in my user ; then I typed this in the url field :

about:support

I found the temporary folder created by Selenium; the folder was :

C:\Users\ADMIN\AppData\Local\Temp\rust_mozprofileilHfxV

I copied / pasted this path in my python script...and it worked !

But working on something placed in a temp folder is risky : one day or another it will be deleted.

So I moved the rust_mozprofileilHfxV folder in a folder of my choice and gave this way to firefox_profile option and...it worked too !

It apperas that profiles created with Selenium have some kind of fingerprint (because made by rust). If Selenium does not see it, it creates a new profile.

Another strange thing, it seems that some cookies (are they cookies ?) are not loaded within this selenium profile. My account is still logged in, but I checked and configure few options on the pages ; in a normal session in Firefox, it keeps them and if I leave and come back. But with this "rust_made_by_selenium" profile, it does not keep them.

I could handle it by scraping of course.

Here was my experience !

Lyric Tropsed
  • 111
  • 1
  • 4