Search code examples
javascriptpythonseleniumsession-cookies

Is there any easy way to get "value" of a cookie and save it in a .txt file using python?


I am trying to get cookies from a website to automate certain requests. Currently, I'm using selenium's get_cookie function to get the cookie.

driver.get_cookie("cookie-name-here")

This returns a dict in the following format:

{'domain': 'website-name-here', 'expiry': 29630, 'name': 'cookie-name', 'path': '/', 'value':'the-string-that-I-need'}

I only need the data under the 'value' attribute. If there is any easier way to get value in JS, I'm open to that as well.


Solution

  • If I have a dict like this :

    cookies = {'domain': 'website-name-here', 'expiry': 29630, 'name': 'cookie-name', 'path': '/', 'value':'the-string-that-I-need'}
    

    the easiest way would be to get the value is to use .get

    val = cookies.get('value')
    print(val)
    

    or in other words, the below code also should return the value.

    print(driver.get_cookie("cookie-name-here").get('value'))