简体   繁体   中英

Python Selenium: How to get cookies and format them to use in an http request

I am wondering the best way to get the cookies from a selenium webdriver instance (chromedriver), and convert them into a cookie string that can be passed as an http header. Here is the way I have tried doing it: getting the list of a dictionary per cookie that selenium provides, then manually adding equal signs and semicolons to format it as it would be in the Cookie header. The problem is: this does not work, on the site I'm testing it returns 500 internal server error, which I assume is caused by a. a bad handling of the request, and b. a bad request, specifically the cookie part.

cookies_list = driver.get_cookies()
cookieString = ""
for cookie in cookies_list[:-1]:
    cookieString = cookieString + cookie["name"] + "="+cookie["value"]+"; "

cookieString = cookieString  + cookies_list[-1]["name"] + "="+ cookies_list[-1]["value"]

print(cookieString)

Is there an easier method to do this and/or what is the problem with my formatting the cookie string that doesn't work.

Thank you sincerely for any help.

I found a better way.
For example: If want to fetch expiry of a cookie named '__ivc'

all_cookies=self.driver.get_cookies();
cookies_dict = {}
       for cookie in all_cookies:
           cookies_dict[cookie['name']] = cookie['expiry'] // You can insert expiry/value/domain/priority in the value of dctionary
print(cookies_dict)
print(cookies_dict.get('__ivc'))

Output will look like this:

{'_ga': 1685026815, '__ivc': 1685026816, '_ga_MNGCCS5STP': 1685026815}
1685026816

Use json to dump the cookies into an iterable which you could format:

import json
cookies_list = list(json.dumps(get_cookies))

You can save the current cookies as a python object using pickle. For example:

import pickle
import selenium.webdriver 

driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

and later to add them back:

import pickle
import selenium.webdriver 

driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

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