4

I'm using Selenium in Python with Firefox browser.

I want to save cookies and localStorage between each Selenium sessions. I first tried start Firefox using specified Profile. But it reports me:

DeprecationWarning: Setting a profile has been deprecated. Please use the set_preference and install_addons methods

So I believe there are something replacement that do the same thing for me.

The website I'm working on use cookies and localStorage to keep its session. I searched about how to read and write cookies, and I luckily got some useful codes. But I haven't find out anything useful for localStorage.

Some posts suggest me use execute_script to read / write localStorage. But it looks like that I had to open the website first so I can run execute_script on it. But the website require correct localStorage configured to load correctly. Or in the other word, I need set localStorage before the website get loaded.

So is there any way I can keep localStorage between each Selenium sessions?

tsh
  • 4,263
  • 5
  • 28
  • 47

2 Answers2

1

Loading a profile for FireFox can be done by using Options and adding arguments to that object (for some reason, it doesn't work with set_preference)

Here's a minimal working example of loading a profile:

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options

profile_path = (
    r"/Users/username/Library/Application Support/Firefox/Profiles/yam8l662.testprofile"
)

options = Options()
options.add_argument("-profile")
options.add_argument(profile_path)
driver = Firefox(options=options)
t = driver.get("about:support")
input()  # just so you can read the page
driver.quit()

Loading the about:support page allows you to scroll down a bit and check which profile is being used.

Gugu72
  • 2,052
  • 13
  • 35
-1

I have this working with:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.profile = 'jenny'
options.add_argument('-headless')
browser = webdriver.Firefox(options=options)
browser.get('https://app.slack.com/client')

in that it will open a headless firefox logged in as jenny because I have her profile directory saved in that same directory. i.e.:

python $ ls -l jenny 
total 24008
-rw-r--r--@  1 aa  staff     4535 Jul 11 13:20 AlternateServices.txt
-rw-r--r--@  1 aa  staff     3308 Jul 11 13:19 ExperimentStoreData.json
-rw-r--r--@  1 aa  staff     1046 Jul 11 13:20 SiteSecurityServiceState.txt
-rw-r--r--@  1 aa  staff   185865 Jul 11 13:19 activity-stream.discovery_stream.json
-rw-r--r--@  1 aa  staff     5599 Jul 11 13:20 addonStartup.json.lz4
-rw-r--r--@  1 aa  staff       24 Jul 11 13:20 addons.json
drwxr-xr-x@  2 aa  staff       64 Jul 11 13:19 bookmarkbackups
drwx------@  6 aa  staff      192 Jul 11 13:20 cache2
-rw-------@  1 aa  staff   294912 Jul 11 13:20 cert9.db
-rw-------@  1 aa  staff      221 Jul 11 13:19 compatibility.ini
-rw-r--r--@  1 aa  staff      875 Jul 11 13:19 containers.json
-rw-r--r--@  1 aa  staff   262144 Jul 11 13:19 content-prefs.sqlite
-rw-r--r--@  1 aa  staff   524288 Jul 11 13:20 cookies.sqlite
drwx------@  4 aa  staff      128 Jul 11 13:20 crashes
drwxr-xr-x@  6 aa  staff      192 Jul 11 13:20 datareporting
-rw-r--r--@  1 aa  staff     1295 Jul 11 13:20 extension-preferences.json
-rw-r--r--@  1 aa  staff    41961 Jul 11 13:20 extensions.json
-rw-r--r--@  1 aa  staff  5242880 Jul 11 13:20 favicons.sqlite
drwxr-xr-x@  3 aa  staff       96 Jul 11 13:20 features
-rw-r--r--@  1 aa  staff   262144 Jul 11 13:20 formhistory.sqlite
-rw-r--r--   1 aa  staff        0 Jul 11 13:16 geckodriver.log
drwxr-xr-x@  3 aa  staff       96 Jul 11 13:20 gmp-gmpopenh264
drwxr-xr-x@  3 aa  staff       96 Jul 11 13:20 gmp-widevinecdm
-rw-r--r--@  1 aa  staff      410 Jul 11 13:19 handlers.json
-rw-------@  1 aa  staff   294912 Jul 11 13:19 key4.db
drwx------@  2 aa  staff       64 Jul 11 13:19 minidumps
-rw-r--r--@  1 aa  staff    98304 Jul 11 13:20 permissions.sqlite
-rw-------@  1 aa  staff      458 Jul 11 13:19 pkcs11.txt
-rw-r--r--@  1 aa  staff  5242880 Jul 11 13:20 places.sqlite
-rw-------@  1 aa  staff    11755 Jul 11 13:20 prefs.js
-rw-r--r--@  1 aa  staff    65536 Jul 11 13:20 protections.sqlite
drwxr-xr-x@ 31 aa  staff      992 Jul 11 13:20 safebrowsing
drwx------@  3 aa  staff       96 Jul 11 13:20 saved-telemetry-pings
-rw-r--r--@  1 aa  staff      365 Jul 11 13:19 search.json.mozlz4
drwxr-xr-x@  2 aa  staff       64 Jul 11 13:19 security_state
-rw-r--r--@  1 aa  staff      288 Jul 11 13:20 sessionCheckpoints.json
drwxr-xr-x@  2 aa  staff       64 Jul 11 13:20 sessionstore-backups
-rw-r--r--@  1 aa  staff    12263 Jul 11 13:20 sessionstore.jsonlz4
drwxr-xr-x@  4 aa  staff      128 Jul 11 13:20 settings
-rw-r--r--@  1 aa  staff       18 Jul 11 13:19 shield-preference-experiments.json
drwxr-xr-x@  7 aa  staff      224 Jul 11 13:20 startupCache
drwxr-xr-x@  7 aa  staff      224 Jul 11 13:20 storage
-rw-r--r--@  1 aa  staff     4096 Jul 11 13:20 storage.sqlite
drwxr-xr-x@  2 aa  staff       64 Jul 11 13:19 thumbnails
-rw-r--r--@  1 aa  staff       50 Jul 11 13:19 times.json
-rw-r--r--@  1 aa  staff    98304 Jul 11 13:20 webappsstore.sqlite
-rw-r--r--@  1 aa  staff      245 Jul 11 13:20 xulstore.json

This is done with

/Applications/Firefox.app/Contents/MacOS/firefox --profilemanager

You can pick the directory for the profile and name it.

But it still get that:

DeprecationWarning: Setting a profile has been deprecated. Please use the set_preference and install_addons methods

and can't seem to get to it work with whatever is the correct install_addons methods.

Andrew Arrow
  • 4,248
  • 9
  • 53
  • 80