Glitchy_messG
Apify & Crawlee7mo ago
18 replies
Glitchy_mess

Using browser_new_context_options with PlaywrightBrowserPlugin

Hello,
I'm very confused on how to use browser_new_context_options because the error code that i get implies that storage_state is not a variable that Playwright's new_context call supports. However on the playwright docs, storage_state seems to support uploading cookies as a dictionary, and when I try running playwright by itself and uploading cookies through this method, everything works fine.

A lot of the cookie related questions in this forum seems to be before the msot recent build, so I was wondering what the syntax should be to properly load cookies through the PlaywrightBrowserPlugin class, as everything else seems to work just fine once that's sorted.

TLDR: what's the syntax for using browser_new_context_options so that it can load a cookie that's set up as a dictionary?

#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])
image.png
image.png
Solution
figured out the issue in dms, for anyone looking up this sort of issue in the future, you'd want to have something akin to this script
some lines of interest are:
"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
Was this page helpful?