22

I try to use Selenium Webdriver and Python on Windows 10 system to make some automation of browser actions. But I have this problem: Selenium-started Firefox window doesn't "see" that I am already logged in and target site sends me to login page. So I assumed that Selenium not really uses the profile, but just a copy of it.

I would like to know:

  1. Is my conclusion about actual use of copy of profile true?
  2. If 1. is true, is there a way to use really everything from existing profile?
  3. If my conclusion is not true, please prove it and point me to the direction where I can find out what information can be used for session, why Selenium could fail to send it and how to force it to do so actually.

Edit:

from selenium import webdriver
fp = webdriver.FirefoxProfile('C:/Users/<user name>/AppData/Roaming/Mozilla/Firefox/Profiles/abc3defghij2.ProfileName')
driver = webdriver.Firefox(fp)
driver.get("https://www.example.com/membersarea")
Grokify
  • 15,092
  • 6
  • 60
  • 81
svgrafov
  • 1,970
  • 4
  • 16
  • 32
  • are you calling any saved profile or browser in script? plz provide code – murali selenium May 16 '16 at 05:54
  • Why do you want Selenium to "detect" that you're already logged-in, when you can assume a "brand new" session, and perform the login via Selenium itself? – barak manos May 16 '16 at 08:37
  • I used Selenium IDE with SelBlocks for a similar task, but looks like I met one of its limitations - I needed to get information from one page while logged in and not logged in. As far as I know, with Selenium IDE I can open private window using javascript, but cant work with it. – svgrafov May 16 '16 at 09:15

1 Answers1

23

Selenium indeed uses a copy of the profile, though that ought not to cause any problems. I think your issue has more to do with session cookies vs. persistent cookies.

On support.mozilla.org is a list indicating what information is actually stored in your profile. Note that cookies are among these, however session-cookies are not stored in cookies.sqlite which is the reason Selenium cannot rebuild your session since it does not appear in the profile.

Many sites, however, offer a remember-me or a stay-logged-in option on their login page which, if used, will store a persistent cookie by which the session can be restored. I used the following script to test this out with gmail,

from selenium import webdriver

url = "https://mail.google.com"
fp = webdriver.FirefoxProfile('/Users/<username>/Library/Application Support/Firefox/Profiles/71v1uczn.default')

driver = webdriver.Firefox(fp)
driver.get(url)

When I run this script after having logged into gmail with the stay-logged-in option enabled, then Selenium is able to access my inbox. If the stay-logged-in option is not enabled the session is destroyed upon closing my browser and thus Selenium cannot restore it either.

The screenshot below shows that session cookies are indeed not stored in cookies.sqlite and thus do not appear in the profile when used by Selenium.

Firefox cookies in cookies.sqlite and firebug

sowa
  • 1,249
  • 16
  • 29
  • Thanks for the answer! However, I couldn't find the option `stay-logged-in` on Gmail page, has it been removed? – eirenikos Aug 13 '18 at 15:13
  • 8
    you can find your active profile-folder by using `about:support` in firefox – Skandix Oct 24 '18 at 12:27
  • This is very nice of you – hoymkot Jul 03 '21 at 23:48
  • There are workarounds. 1. patch local webdriver to don't use a copy folder. 2. retrieve copy back, reading the __path__ property of the initialized driver's fp. 3. Start browser manually with marionette enabled, start a instance of geckodriver that has --connect-existing, then connect using the remote_driver using local url. Using the 3rd option you are using a real profile all the time. Also remember to remove /tmp/rust* and tmp* files, which are copies of your older profiles. Example of retrieving back profile here: https://stackoverflow.com/a/33350778/2480481 – m3nda Dec 11 '22 at 09:17