Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to load cookies with Pickle
#1
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.
Reply
#2
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)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
THANK YOU DADDY! IT WORKED!!!
Reply
#4
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
Reply
#5
(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.
Reply
#6
To add cookie back into selenium there is browser.add_cookie(cookie)
for cookie in saved_cookies:
    browser.add_cookie(cookie)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python cookies vj78 2 1,689 Apr-07-2021, 09:38 AM
Last Post: Naheed
  Catch all cookies from any website Chuky 8 5,126 Aug-20-2019, 04:24 PM
Last Post: snippsat
  python scraping all cookies halberdd 1 2,977 Feb-26-2019, 12:10 PM
Last Post: halberdd
  Flask: Cookies for Saving User Input ? jomonetta 2 2,060 Nov-03-2018, 10:47 AM
Last Post: j.crater
  Inserting multiple cookies using python requests freshtomatoes 0 3,536 Aug-16-2018, 09:25 PM
Last Post: freshtomatoes
  unable to load file using python selenium purnima1 4 4,950 Dec-12-2017, 04:04 PM
Last Post: hshivaraj
  Missing cookies in session with requests sechot 0 4,886 Oct-22-2017, 12:09 PM
Last Post: sechot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020