How to save Firefox webdriver session in python

Viewed 1273

To save chromedriver session, I used this snippet of code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('user-data-dir= path to where to save session')
driver = webdriver.Chrome(executable_path='path to chromedriver.exe', chrome_options=options)

I tried to do the same thing with Firefox but it doesn't seem to work:

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

options = Options()
options.add_argument('user-data-dir= path to where to save session')
driver = webdriver.Firefox(executable_path='path to geckodriver.exe', firefox_options=options)

Is this the right way to go or did I miss something?

1 Answers

Below is the manual process to create a new profile and start firefox using existing profile:

  1. To create a new profile, execute command: firefox.exe -CreateProfile JoelUser

  2. To create a new profile in another directory, execute command: firefox.exe -CreateProfile "JoelUser c:\internet\joelusers-moz-profile"

  3. To start firefox using new profile, execute command: firefox.exe -P "Joel User"

Now, to achieve the same programmatically, I figured out that step no. 1 or 2 can be executed using subprocess and step no. 3 can be achieved by following https://stackoverflow.com/a/54065166/6278432

References:

  1. firefox bug - unable to create new session using custom profile: https://github.com/mozilla/geckodriver/issues/1058
    https://bugzilla.mozilla.org/show_bug.cgi?id=1421766

  2. Firefox command line params - https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#user_profile

  3. subprocess api doc - https://docs.python.org/3/library/subprocess.html

Related