Change default profile saving directory - Selenium Firefox

Viewed 34

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,

2 Answers

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