Accessing browser sessionStorage within Puppeteer automation scripts

I’m working on a Puppeteer automation that needs to extract a specific value from the browser’s session storage. My script launches a web browser and I need to grab an encounter ID that gets stored during the session.

When I try to run this code directly in my Puppeteer script:

let encounterId = sessionStorage.getItem('encounter-session-id');

I get an error saying that sessionStorage is not defined. I’m running these automation scripts through AWS synthetic monitoring.

How can I properly access the browser’s sessionStorage from within my Puppeteer code? Is there a specific method I need to use to interact with the browser’s storage APIs?

This happens because sessionStorage only works in the browser, not in your Node.js Puppeteer script. You need to run the code inside the browser using page.evaluate(): const encounterId = await page.evaluate(() => { return sessionStorage.getItem(‘encounter-session-id’); }); I’ve used this pattern tons of times in automation projects - it works great. The page.evaluate() method runs your function in the browser where sessionStorage actually exists. Don’t forget to await it since it returns a promise. Also, make sure the sessionStorage item is set first, or you’ll just get null.

This is a common Puppeteer issue. You’re trying to access sessionStorage from Node.js, but it only exists in the browser. Use page.evaluate() to run code in the browser context: const encounterId = await page.evaluate(() => sessionStorage.getItem('encounter-session-id'));. Watch out for timing issues - sessionStorage might not be populated yet. Add a wait condition or check if the value exists first. AWS synthetic monitoring is picky about timing, so throw in some error handling too.

yeah, this error’s super common with puppeteer. you’re trying to call browser apis directly from node, which doesn’t work. wrap your sessionStorage call in page.evaluate() like everyone else said. also try page.waitForFunction() if that storage value doesn’t show up right away - sometimes the page loads before javascript actually sets the sessionStorage.