Python Forum

Full Version: Unable to load cookies with Pickle
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Full Code:
>>> while True:
	from selenium import webdriver
	driver = webdriver.Chrome()
	import pickle
	cookies = pickle.load(open("cookies.pkl", "rb"))
	while True:
		for cookie in cookies:
			driver.add_cookie(cookie)
		break
	break

Traceback (most recent call last):
  File "<pyshell#1>", line 8, in <module>
    driver.add_cookie(cookie)
  File "C:\Users\Arbi717\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 772, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "C:\Users\Arbi717\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Arbi717\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unable to set cookie
  (Session info: chrome=63.0.3239.84)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.16299 x86_64)

>>>
I have previously saved cookies with:
>>> pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
It worked once, however now it is not working. I tried deleting the cookies.pkl file and recreating it again, but to no avail. Any help is appreciated.
Seems like it open file and not closing file.
When you open a file. You must close it to save it.
If you don't close file. It a memory leak.

with open('cookies.pkl'.'rb') as cookie_file: # with auto close file.
    cookies = pickle.load(cookie_file)
THANK YOU DADDY! IT WORKED!!!
You seems to do some do some strange stuff like running all in a while loop for interactive interpreter(>>>).
The while loop is not needed as Selenium run continuous until call browser.quit().
Here a example,and i use json because i don't like pickle.
from selenium import webdriver
import json

browser = webdriver.Chrome()
url = 'https://www.google.com/'
browser.get(url)
cookie = browser.get_cookies()
browser.quit()

with open('cookie', 'w') as j_out:
    json.dump(cookie, j_out)
with open('cookie') as j:
    saved_cookie = json.load(j)


#print(saved_cookie) # The whole cookie as a list with dictionaries 
print(saved_cookie[0]['domain'])
print(saved_cookie[0]['httpOnly'])
Output:
.google.no False
(Jan-04-2018, 03:05 AM)snippsat Wrote: [ -> ]You seems to do some do some strange stuff like running all in a while loop for interactive interpreter(>>>).
The while loop is not needed as Selenium run continuous until call browser.quit().
Here a example,and i use json because i don't like pickle.
from selenium import webdriver
import json

browser = webdriver.Chrome()
url = 'https://www.google.com/'
browser.get(url)
cookie = browser.get_cookies()
browser.quit()

with open('cookie', 'w') as j_out:
    json.dump(cookie, j_out)
with open('cookie') as j:
    saved_cookie = json.load(j)


#print(saved_cookie) # The whole cookie as a list with dictionaries 
print(saved_cookie[0]['domain'])
print(saved_cookie[0]['httpOnly'])
Output:
.google.no False

In what order do I do all of this? I tried the first method and that worked once, however it is no longer working. I want to try to get this using json, but can you explicitly tell me how to go about creating a cookie? Right now this is what I do:
from selenium import webdriver
import json

browser = webdriver.Chrome()
url = 'https://www.google.com/'
browser.get(url)
# I OPEN ANOTHER TAB AND LOG INTO MY ACCOUNT ON GOOGLE AND ON THAT WEBSITE
cookie = browser.get_cookies()
browser.quit()
# Now I close python and open it again

from selenium import webdriver
import json
browser = webdriver.Chrome()
cookie = browser.get_cookies()
url = 'https://www.google.com/'
with open('cookie', 'w') as j_out:
    json.dump(cookie, j_out)
with open('cookie') as j:
    saved_cookie = json.load(j)
browser.get(url)
However, the cookies do not save. This I know because I am not logged into google.
To add cookie back into selenium there is browser.add_cookie(cookie)
for cookie in saved_cookies:
    browser.add_cookie(cookie)