The task is simple. Go to the service https://mail.ru/ Load cookies. Go to the subservice with the same cookies https://x.calendar.mail.ru/
Manually - it works. As soon as they are loaded from a file, when switching to the second service, the cookies are deleted. I don't understand what the problem is?
Code:
import undetected_chromedriver as uc
from selenium import webdriver
import time
import os
import pickle
from utils.navigate import navigate, coockies_load
options=webdriver.ChromeOptions()
driver = uc.Chrome(options=options)
def main():
navigate(driver, "https://mail.ru/", 20)
coockies_load(driver)
navigate(driver, "https://x.calendar.mail.ru/", 5)
if __name__ == '__main__':
main()
File navigate.py
import os
import time
import pickle
import time
def navigate(driver, url: str, sec: int):
driver.get(url)
time.sleep(sec)
def coockies_load(driver):
if os.path.exists('cookies.pkl'):
for cookie in pickle.load(open("cookies.pkl", "rb")):
driver.add_cookie(cookie)
print('Загрузили файл куки')
time.sleep(5)
driver.refresh()
time.sleep(5)
else:
navigate(driver, "https://mail.ru/", 5)
pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))
print('Сохранили файл куки')
time.sleep(5)