0

I want to keep Firefox Login status when using Selenium WebDriver (geckodriver). I used below python code:

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

try:
    fp = webdriver.FirefoxProfile('C:/Users/admin/AppData/Roaming/Mozilla/Firefox/Profiles/mow8ff69.selenium')
    options = Options()
    options.headless = False
    browser = webdriver.Firefox(fp, options=options)
    browser.get('https://stackoverflow.com/users/login');
    time.sleep(2)

    txt_username = browser.find_element_by_id('email')
    txt_username.clear()
    txt_username.send_keys('***@gmail.com')  #replace with your own username
    time.sleep(1)
    txt_password = browser.find_element_by_id('password')
    txt_password.clear()
    txt_password.send_keys('*****')   #replace with your own password
    time.sleep(1)
    txt_password.submit()
    time.sleep(10)

except Exception as ex:
    print('Error: {0}'.format(ex))
finally:
    browser.quit()

As shown above, I have been using a User Profile created by "Firefox -p". However, after running above code, when I reopen Firefox using that profile and then go to stackoverflow.com, I'm still not logged in.

So it looks like that Selinum WebDriver actually runs firefox in a "sandbox" which cannot write to the User Profile at all? I know that ChromeDriver has a "--no-sandbox" option but it doesn't seem to be available in geckodriver? Is there any workaround?

I cannot switch to ChromeDriver because there is another bug with it: the "--headless" option actually conflicts with "--user-data-dir" option whereas I really need both.

  • Does this answer your question? [Selenium use of Firefox profile](https://stackoverflow.com/questions/37247336/selenium-use-of-firefox-profile) – ChrisGPT was on strike Jan 05 '21 at 01:32
  • HI Chris - thanks for the comment. However what I needed is still a bit different. That thread did explain that selenium uses a copy of the profile - so that making cookies persistent will help Selenium to access the cookies stored in the profile. However, what I wanted is that the changes to the cookies made by Selenium to be applied back to the user profile. Is there anyway to do so? If I know the location of the copy of the user profile I can simply copy it back to the original user profile location? – Jungle Jiang Jan 05 '21 at 02:20

0 Answers0