Glitchy_mess
06/26/2025, 7:23 AM#cookie dictionary is just a dictionary of cookie parameters that is formatted per #playwright's preferred format
browserPlugin = PlaywrightBrowserPlugin(
browser_new_context_options={"storage_state": {"cookie": cookieDictionary}}
)
browserPoolVar = BrowserPool(plugins=[browserPlugin])
https://cdn.discordapp.com/attachments/1387694806897000529/1387694807174086667/image.png?ex=685e4700&is=685cf580&hm=4e4c893a63f9d2cd430af9a6eadef9537a6672eb6f55aed831f33d4dcf8a1358&
https://cdn.discordapp.com/attachments/1387694806897000529/1387694807694049300/image.png?ex=685e4700&is=685cf580&hm=3a036b370f6739c79e98679c31655f8938932448b178776fe63cd1556441ba82&Answer Overflow
06/26/2025, 7:23 AMmatyascimbulka
06/26/2025, 7:47 AMMantisus
06/26/2025, 10:20 AMPlaywrightCrawler in basic mode uses [launch_persistent_context](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context), which has no storage_state parameter.
The standard BrowserContext is only used when the use_incognito_page flag is set.Glitchy_mess
06/26/2025, 3:39 PMGlitchy_mess
06/26/2025, 3:49 PMuse_incognito_page flag does make the request go through without any errors, but for some reason the cookies itself don't seem to register since i'm still set on the sign in screen for the website i'm crawling
(if it matters, it's this page https://sellercentral.amazon.ca/help/hub/reference/G200164490, afaik they don't have any anti-scraping things in place since playwright is able to load the cookies just fine)Mantisus
06/26/2025, 4:16 PMdummyCookie, they didn't install because instantly expire.
Playwright use -1 for cookies that should not expire
python
async def main() -> None:
dummyCookie = {
"name": 'abcd',
'value': 'xyz',
'domain': '.httpbin.org',
'path': '/',
'expires': -1, # Note!
'httpOnly': True
}
browserPoolVar = BrowserPool()
crawler = PlaywrightCrawler(
use_session_pool=True,
max_session_rotations=0,
browser_pool=browserPoolVar,
session_pool=SessionPool(max_pool_size=1)
)
@crawler.pre_navigation_hook
async def pre_navigation_handler(context: PlaywrightPreNavCrawlingContext) -> None:
context.session.cookies.set_cookies_from_playwright_format([dummyCookie])
@crawler.router.default_handler
async def request_handler(context: PlaywrightCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url}...')
currentPage = await context.page.content()
print(currentPage)
await crawler.run(['https://httpbin.org/cookies'])
Correctly set cookies before navigationGlitchy_mess
06/26/2025, 4:39 PMcontext.session.cookies.set_cookies_from_playwright_format([dummyCookie])
this error gets thrown:
ValueError: dictionary update sequence element #0 has length 6; 2 is required
i think the ValueError is referring to "signin-sso-state-ca" as the element #0, but that's 19 characters long, and i don't know where you'd find the the 2 length element that playwright is looking for there
also i added two errors that pylance is throwing. I'm not sure what either mean in this context , but i don't think pylance really knows how to handle crawlee so it might be nothing?
https://cdn.discordapp.com/attachments/1387694806897000529/1387834629368709210/image.png?ex=685ec938&is=685d77b8&hm=a61164a2e9700491fc2af519a48cae331b2157ffafded9fc2c1521c0b4dd075c&
https://cdn.discordapp.com/attachments/1387694806897000529/1387834629842669669/image.png?ex=685ec938&is=685d77b8&hm=e4dee21258ca5ee92f2590be3447ac1b0696413847cc55744dc2da4248409400&Mantisus
06/26/2025, 4:53 PMNone and pylance wait
python
if context.session:
context.session.cookies.set_cookies_from_playwright_format([dummyCookie])
is due to the fact that the set_cookies_from_playwright_format method is used mostly inside the library and is typed via TypedDict. But this typing is not available through public importsMantisus
06/26/2025, 5:01 PMGlitchy_mess
06/26/2025, 5:05 PMset_cookies_from_playwright_format error then since the typing isn't available from public imports
> don't try to set a cookie that expired an hour ago
oh true true, give me a sec to see if clearing the cookies and cache and resigning in (which should hopefully refresh cookies?) helpsGlitchy_mess
06/26/2025, 5:12 PMApifyBot
06/26/2025, 5:12 PMMantisus
06/26/2025, 5:13 PMsession.cookies.set_cookies is available, but its format is slightly different from Playwright's.
python
import asyncio
from crawlee.crawlers import PlaywrightCrawler, PlaywrightCrawlingContext, PlaywrightPreNavCrawlingContext
from crawlee.sessions import SessionPool
from crawlee.browsers import BrowserPool
from crawlee.sessions import CookieParam
async def main() -> None:
dummyCookie = CookieParam({
"name": 'abcd',
'value': 'test',
'domain': '.httpbin.org',
'path': '/',
'expires': 1751042637,
'http_only': True
})
browserPoolVar = BrowserPool()
crawler = PlaywrightCrawler(
use_session_pool=True,
max_session_rotations=0,
browser_pool=browserPoolVar,
session_pool=SessionPool(max_pool_size=1)
)
@crawler.pre_navigation_hook
async def pre_navigation_handler(context: PlaywrightPreNavCrawlingContext) -> None:
if context.session:
context.session.cookies.set_cookies([dummyCookie])
@crawler.router.default_handler
async def request_handler(context: PlaywrightCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url}...')
currentPage = await context.page.content()
print(currentPage)
await crawler.run(['https://httpbin.org/cookies'])
if __name__ == '__main__':
asyncio.run(main())
Will clear a typing errorMantisus
06/26/2025, 5:15 PMGlitchy_mess
06/26/2025, 5:16 PMGlitchy_mess
06/26/2025, 5:16 PMGlitchy_mess
06/26/2025, 6:54 PM"expires": int(parts[4]) if parts[4] else None, #type: ignore
"http_only": parts[3].lower() == 'true',
"secure": parts[5].startswith('__')
where you want to clean up the expires and http_only fields so that they don't conflict with playwright's formatting, and "secure" ensures that any cookies that start with a __ are formatted so that they are https only, as opposed to just looking for http, since playwright doesn't like the __
beyond that, lines 38-44 are useful for logging whether a cookie has failed to load or not
also also major props to Mantisus for writing the script itself, i'm just dropping it here before closing the thread
https://cdn.discordapp.com/attachments/1387694806897000529/1387868684902731887/testScriptForDisc.py?ex=685ee8ef&is=685d976f&hm=7d31cd18dbdac122335f802d2b8a4a3ab8f96de317a089ab4b02940f7f3a99d4&