Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support cookies output format from Chrome DevTools Storage.getCookies #186

Open
anasfanani opened this issue Apr 24, 2024 · 0 comments

Comments

@anasfanani
Copy link

anasfanani commented Apr 24, 2024

When using automation task in selenium, I can get all browser cookies with this python command:

all_cookie = driver.execute_cdp_cmd('Storage.getCookies',{})

And this for set the cookie :

driver.execute_cdp_cmd('Network.enable', {})
driver.execute_cdp_cmd('Network.setCookie', cookie)
driver.execute_cdp_cmd('Network.disable', {})

Output Example is:

[
{
        "domain": ".so.sites.com",
        "expires": 1747602543,
        "httpOnly": false,
        "name": "bcap",
        "path": "/",
        "priority": "Medium",
        "sameParty": false,
        "sameSite": "unspecified",
        "secure": true,
        "session": false,
        "size": 6,
        "sourcePort": 443,
        "sourceScheme": "Secure",
        "value": "0"
    }
]

The structure of cookies is described here : https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-Cookie

This would be verry usefull.

Currently I'm use this code to export:

def exportCookie(cookies):
        output = []
        for cookie in cookies:
            cookie_dict = {}
            cookie_dict['domain'] = cookie.get('domain')
            if cookie.get('expires') != -1:
                cookie_dict['expirationDate'] = cookie.get('expires')
            cookie_dict['hostOnly'] = not cookie.get('domain', '').startswith('.')
            cookie_dict['httpOnly'] = cookie.get('httpOnly')
            cookie_dict['name'] = cookie.get('name')
            cookie_dict['path'] = cookie.get('path')
            sameSite = cookie.get('sameSite')
            if sameSite is None or sameSite == 'unspecified' or sameSite == 'None':
                cookie_dict['sameSite'] = 'no_restriction'
            else:
                cookie_dict['sameSite'] = sameSite.lower()
            cookie_dict['secure'] = cookie.get('secure')
            cookie_dict['session'] = cookie.get('session')
            cookie_dict['storeId'] = None
            cookie_dict['value'] = cookie.get('value')
            output.append(cookie_dict)
        output = json.dumps(output, indent=4)
        return output

Update, I'm suprissed when try to export with many domain, can't set the cookies, the permission is for all site, but only opened tab can set the cookies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant