Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Getting the cookie with "path" field #3630

leesache started this conversation in General
Discussion options

Hello, SB Community and Michael
My current goal is to get the cookie in browser, which has a "path" field. Which doesn't allow me to get that cookie if I'm not on the corresponding path (e.g. v2/auth) I've tried a lot of approaches, but redirecting to that path can't help me, because this exact cookie is deleted when I redirect to any page. (Important)

The only approach that can work is to enable CDP mode, (which I would like not to do, but it seems that there is no solutions).
I'm activating CDP In big application

async def registration_scenario(
 api_key: str, service: str, user_id: int, is_male: bool = True
) -> str | Exception:
 platform: str = get_platform()
 user_agent: str = get_user_agent(platform)
 window_size: str = get_window_size()
 with SB(
 uc=True,
 browser="chrome",
 locale="ru",
 position="0,0",
 agent=user_agent,
 window_size=window_size,
 proxy=proxy,
 block_images=True,
 ) as driver:
 manager = RegistrationManager(
 driver, api_key, service, is_male, user_id, platform
 )
 result = await manager.execute_queue()
 return result

And at some point to get that cookie I need to activate CDP mode, it will be called but from async function but if I call it, it says that there is already another event loop. Is there any solution to this problem? Or maybe you can suggest me a way how can I extract that cookie from chromium.
Also will it even work if I enable CDP mode while running the code, if activate_cdp_mode() opens a new instance of browser?

You must be logged in to vote

Replies: 3 comments

Comment options

Also this cookie is not only in cookie for one time, it's also can be found in request headers.

You must be logged in to vote
0 replies
Comment options

The available cookie methods for CDP Mode are:

sb.cdp.get_all_cookies(*args, **kwargs)
sb.cdp.set_all_cookies(*args, **kwargs)
sb.cdp.save_cookies(*args, **kwargs)
sb.cdp.load_cookies(*args, **kwargs)
sb.cdp.clear_cookies()
sb.cdp.get_cookie_string()

There's a CDP Mode cookie example here:

Do browser navigation after activating CDP Mode, as the URL changes on activation.

You must be logged in to vote
0 replies
Comment options

The best approach we found is to just

sb.execute_cdp_cmd('Storage.getCookies', cmd_args={})

Can get all the cookies, even the cookies which can be accessed only on specified path.
Doesn't need to enable CDP mode for using.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /