I use this code to get cookies and save them to file
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import os
import requests, pickle
user = os.getlogin()
options = webdriver.ChromeOptions()
# options.headless = True
options.add_argument('--profile-directory=Default')
options.add_argument(
f'--user-data-dir=C:\\Users\\{user}\\AppData\\Local\\Google\\Chrome\\User Data')
PATH = os.path.dirname(__file__) "\\chromedriver.exe"
driver = webdriver.Chrome(
executable_path=PATH, chrome_options=options)
driver.get("https://web.whatsapp.com")
WebDriverWait(driver, 5000).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="side"]/header/div[2]/div/span/div[2]/div')))
cook = driver.get_cookies()
pickle.dump( cook , open("cookies.pkl","wb"))
but when I want to read them give me a Error
code :
session = requests.session()
with open('cookies.pkl', 'rb') as f:
session.cookies.update(pickle.load(f))
Error :
for key, value in other:
ValueError: too many values to unpack (expected 2)
Thanks again if anyone can recommend a better way to read and write cookies in the file
CodePudding user response:
Your cookies.pkl
file contains a list while session.cookies.update
expects a dictionary, so either iterate over your list and update multiple times or pick the correct cookies form the list and update using that