简体   繁体   中英

I'm unable to load cookies into a headless chrome session + selenium (MacOS)

I'm trying to load cookies from a previous session into a new headless chrome session using selenium, if i try to load them using the same method i do with chrome but not in headless mode and taking a screen shot, it doesn't work because instead of being logged into the site takes me to the login screen.

I've already tried passing via Options() a cookies folder or saving them with pickle and uploading once to the site like that:

options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get('site')

cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

driver.refresh()

but neither one worked.

I have already searched if anyone has ever encountered my problem, but all the question is about loading cookies with selenium in a normal chrome session.

Thanks to all reply.

You have to be on another site, load cookies and then redirect to the desired page.

options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get('other_site')

cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

driver.get('site')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM