0

I am trying to check if my cookie is outdated.

I am testing on Facebook.com. I want to avoid logins for every attempt. but when the cookie is outdated all the web pages configurations and classes change so this is why I'm trying to keep my cookie updated.

Mainly I get the cookies from inspect element -> storage > copy pasted everything under facebook.com to a CSV file and then adding each line of this CSV file to the browser using

def addCookie(self, file):
    with open(file) as f:
        dict_read = DictReader(f)
        list_of_dicts = list(dict_read)
    return list_of_dicts

when the script starts.

The code in the main is:

cookies = bot.addCookie(
    "facebook_cookies.csv")

for _ in cookies:
    bot.add_cookie(_)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To avoid logging in for every attempt and at the same time keep the updated, you can store the cookies everytime you finish your task and and reuse them while logging in next time using pickle module as follows:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pickle

# first time logging in
driver.execute("get", {'url': 'http://www.example.com'})
# login steps followed by storing the cookies
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
driver.quit()

# next time logging in
driver = webdriver.Chrome(service=s, options=options)
driver.execute("get", {'url': 'http://www.example.com'})
# using the stored cookies to log in
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
driver.execute("get", {'url': 'http://www.example.com'})

# updating the cookies before terminating
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
driver.quit()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352