5

I need to transfer my Selenium session between 2 computers.

To export and import the cookies via Selenium you have to visit each website individually before you can set its cookies, and I want to avoid that. I also want to copy over the local storage.

Here's what I've tried so far:

1 - Launch a fresh Selenium session:

driver = webdriver.Chrome()

2 - Find its temporary Chrome user profile in %temp% and copy it over to my application's folder

3 - Launch a new driver using this user profile:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("user-data-dir=C:\\profiles\\temp_profile")
driver = webdriver.Chrome(chrome_options=chrome_options)

The above works - the local storage and the cookies are still there on my local PC, however, when I copy this user profile to another PC and launch it, then the local storage is still there, but the cookies are gone.

I have also tried the same using a regular chrome profile, as well as trying to launch the user profile directly in Chrome instead of Selenium, and the cookies are still gone.

Bart
  • 524
  • 7
  • 17

3 Answers3

3

Since the cookies are stored in a SQLite file for Chrome, and they're encrypted, you don't really know (or want to dig in to..) why it doesn't work. It probably has something to do with information security.

So my suggestion would be to save them using WebDriver's functionality and load them in-memory. See here.

Community
  • 1
  • 1
Moshisho
  • 2,781
  • 1
  • 23
  • 39
  • This works, but you have to open the website before you can load cookies for that domain - I was hoping to do that in advance. – Bart Feb 24 '17 at 19:56
  • 1
    Then I suggest you do open up the SQLite Cookies file in both PCs to see the differences, could be you need to replace some data in some column to fit the 2nd PC. BTW, is your session on localhost? – Moshisho Feb 24 '17 at 20:14
  • The Cookies file has only the cookies, except that the values are encrypted. Don't see anything PC-specific. Selenium is on localhost but the websites are not. I could maybe temporarily bind the websites to localhost and visit them like that lol. – Bart Feb 24 '17 at 20:27
  • 1
    ;) Yeah, I meant is your **host** on localhost... I had to create custom history for Chrome once and the catch was to set the timestamp correctly... maybe that's the problem. Anyways, I'll try to come up with something, it's an interesting case. – Moshisho Feb 24 '17 at 20:50
2

one solution i use is to pickle the cookies and share them between computers

#save cookies
import pickle
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("detach",True)
options.add_experimental_option('prefs', {'credentials_enable_service': False, 'profile': {'password_manager_enabled': False}})
driver = webdriver.Chrome(options=options,executable_path="./chromedriver")
# do login steps, so cookies can be set
pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))

and then you can on the other side, load the cookies

#load cookies
import pickle
from selenium import webdriver
driver.get("http://www.example.com")
cookies = pickle.load(open("cookies.pkl","rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

and the rsync/scp the cookies.pkl file between computers

0

I'm responding this since it came up in my google search.

Maybe it has something to do with the way Chrome encrypts it's cookies, so you would need to use the same certificate on both machines for the copy-paste to work.

Take a look at: https://github.com/bertrandom/chrome-cookies-secure

Using this you still need to know the domain, but you wouldn't need to actually visit the website.

lucaswxp
  • 2,031
  • 5
  • 23
  • 34