Home > database >  Change default profile saving directory - Selenium Firefox
Change default profile saving directory - Selenium Firefox

Time:09-22

I am using selenium with geckodriver, My goal is to use a pre-existing session's profile on a defined path rather than default directory tmp.

As I start session with a new profile:

from selenium import webdriver
profile = webdriver.FirefoxProfile()

profile gets saved by default to directory /tmp

>>> os.system('ls -lt ../../../tmp')
total 25248
drwx------  2 root root     4096 Sep 20 11:10 tmpqzp6uf3c

I'd like to change the default directory /tmp to an another directory I define for any new profile. Thanks,

CodePudding user response:

You can set the initial profile path parameter in the FirefoxProfile([profile_directory]) constructor(document), but the firefox driver will create a temporary profile(directory) and copy the intial profile to it each time(see Why Selenium always create temporary Firefox Profiles using Web Driver?), surely you can copy the temparory folder back like this post: Python / Selenium / Firefox: Can't start firefox with specified profile path

CodePudding user response:

Default location for temporary are defined by environnement variable $TMPDIR, see https://firefox-source-docs.mozilla.org/testing/geckodriver/Profiles.html

I set up env variable TMPDIR=/opt/Projects/01_Twitter/tmp , sourced ~/.bashrc, lunched a new session:

from selenium import webdriver
profile = webdriver.FirefoxProfile()

now profile is savec under $TMPDIR:

>>> os.system('ls -lt /opt/Projects/01_Twitter/tmp')
total 0
drwxr-xr-x 49 root root 1666 Sep 21 10:29 rust_mozprofileAGsZ26
...

When I close driver:

driver.close()

Profile is well saved in $TMPDIR, but Firefox keeps creating temporary files for the profile

>>> os.system('ls -lt /opt/Projects/01_Twitter/tmp')
total 0
drwxr-xr-x 49 root root 1666 Sep 21 10:31 rust_mozprofileGnbPp5
drwxr-xr-x 49 root root 1666 Sep 21 10:29 rust_mozprofileAGsZ26
...

The best option is to copy profile into a persisted directory:

>>> os.system('cp -r /opt/Projects/01_Twitter/tmp/rust_mozprofileAGsZ26 .')

and quit the driver

driver.quit()
  • Related