DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

How to Set Session Storage with Puppeteer

Set sessionStorage in Puppeteer after navigation with page.evaluate(), or initialize it before site scripts run with evaluateOnNewDocument().
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use page.evaluate() to set sessionStorage in a page that has already loaded. If the site reads the value while starting up, register the code with page.evaluateOnNewDocument() before navigating so it runs before the page’s scripts.

Set sessionStorage after a page has loaded

sessionStorage belongs to the page’s JavaScript context, so run the Storage API call with Puppeteer’s page.evaluate():

As an Amazon Associate I earn from qualifying purchases.

await page.evaluate((key, value) => {
  sessionStorage.setItem(key, value);
}, 'token', 'example-value');

page.evaluate() executes the function in the page and returns its result; Puppeteer also waits for a promise returned by the function. Pass the key and value as arguments rather than building a JavaScript string. See the Puppeteer Page.evaluate() documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set the value before the site’s scripts run

If application startup code needs to read the value, register an initializer before calling page.goto():

await page.evaluateOnNewDocument((key, value) => {
  sessionStorage.setItem(key, value);
}, 'token', 'example-value');

await page.goto('https://example.com');

Puppeteer documents that the function runs after a document is created but before its scripts run. It also runs when a child frame is attached or navigated. Registering it after a page has loaded does not run it retroactively in that document; register it first, then navigate or reload when startup timing matters. See Puppeteer Page.evaluateOnNewDocument().

Verify the stored value

Read the value back in the page context to confirm that it was written:

const stored = await page.evaluate((key) => {
  return sessionStorage.getItem(key);
}, 'token');

console.log(stored); // 'example-value'

getItem() returns null when the key is absent. If the read fails or returns null, check that the write ran in the intended page and at the right time, then check the origin and storage restrictions below. The page-context behavior is described in the Puppeteer API; storage reads and writes are part of the MDN sessionStorage reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand which page can see the value

Origin: scheme, host, and port

Storage is associated with an origin, which is defined by scheme, host, and port. A value written on one origin is not available to a different origin—even if the pages have similar names or paths. For example, changing from HTTP to HTTPS changes the origin. See the MDN same-origin policy guide.

Tab and page session

sessionStorage is also partitioned by top-level browsing context. A page session survives reloads, but ends when its tab or window session ends; a different tab should not be assumed to share the same storage area. See MDN’s sessionStorage documentation.

BrowserContext isolation

Puppeteer BrowserContexts provide isolation between browser sessions. The Puppeteer browser-management guide specifically notes that cookies and local storage are not shared between contexts. Use separate contexts when automation runs should not share browser state; session storage still follows its own origin and tab boundaries. See Puppeteer’s browser management guide.

Handle storage errors and timing issues

  • SecurityError on access: Check the current page URL and whether it has a valid scheme, host, and port. Storage access may also be blocked by browser policy or unavailable for an opaque origin. The MDN sessionStorage reference describes these restrictions.
  • The app behaves as if the key is missing: If startup code reads the key before your write, move the initializer to evaluateOnNewDocument() and register it before navigation. A later registration does not change what scripts already read.
  • The value disappears on another page: Confirm that the destination has the same scheme, host, and port, and that it is in the same tab session. Session storage is not general cross-origin or cross-tab state.
  • The test sees state from another run: Check that runs use the intended BrowserContext and page. Separate contexts are useful for isolating automation state.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If the goal is a clean screenshot rather than testing application behavior, ScreenshotNeo can return a screenshot from one GET request. It accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. It also offers an MCP server for AI agents, with the tools take_screenshot, get_page_info, and capture_pdf.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

See the ScreenshotNeo API documentation for the request and available options. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Learn about ScreenshotNeo or sign up for 1,000 free screenshots a month, with no card required.

Best Value
The SQL Programming Language: .
  • Used Book in Good Condition

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.