1

I want to add my cookies from IE from a particular website to selenium browser by way of import.

Imports I currently have if they are any help to you.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
if __name__ == '__main__':

Thanks in advance

koeliga
  • 81
  • 1
  • 6

1 Answers1

1

You can save the current cookies as a Python object using pickle. For example:

import pickle
import selenium.webdriver

driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

And later to add them back:

import pickle
import selenium.webdriver

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

Source

Edit: Be careful when you pickle things. This is a great way to have a deserialization vulnerability introduced into your application.

Bryce
  • 71
  • 1
  • 1
  • 8
  • Websites have too many cookies. Its almost impossible to add cookies manually to driver object via json data. This method is very awesome and pratic. I log in manually at once and save cookies for future uses. – Fahri Güreşçi Nov 05 '22 at 14:26