Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playwright get cookie

I need to get a JSSESSIONID cookie from one page and pass it to another. But when I try to fetch cookie with a playwright I got an empty array. Any ideas?

const browser = await playwright[browserType].launch();
const context = await browser.newContext();
        
const page = await context.newPage();
await page.goto(link);
       
const elementHandle = await page.$('#putfocushere');
await elementHandle.type(searchNumber);
console.log(await context.cookies(page.url()))
like image 627
dusan Avatar asked Feb 22 '26 05:02

dusan


2 Answers

Access it through BrowserContext, which you can get through page.context(), e.g.

await page.context().cookies();
like image 87
Gajus Avatar answered Feb 24 '26 19:02

Gajus


Have you tried doing await context.cookies() without specifying any origin? More than likely, this is happening because the value returned by page.url() isn't an exact match. You can try getting all the cookies and then filtering by your domain.

There is also context.storageState() that is now available in playwright, which will return all cookies and localstorage. See here.

like image 44
Nico Mee Avatar answered Feb 24 '26 19:02

Nico Mee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!