User Agents

Web browsers and everyday web tech, explained

Scrape Local Storage with Playwright Headless Browsers

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Scraping local storage with Playwright involves executing JavaScript within the browser context to access the window.localStorage object. Because local storage is a client-side API, it cannot be retrieved via standard HTTP requests or simple HTML parsing; you must use a headless browser to render the page and run a script that extracts the key-value pairs stored by the website. This technique is essential for capturing session tokens, user preferences, or cached application state that is not present in the page source.

What You'll Need

  • Node.js installed on your machine.
  • The Playwright library (npm install playwright).
  • A target URL where the data is stored in local storage.
  • Basic knowledge of JavaScript and asynchronous functions.

Step-by-Step

  1. Initialize the Playwright Environment: Start by importing the necessary modules. You will need chromium (or firefox/webkit) and the playwright launch method to instantiate the browser instance.
  2. Launch the Browser: Use playwright.chromium.launch(). While you can set headless: false for debugging to see the browser in action, set it to true for production scraping to save system resources.
  3. Create a New Browser Context: Use browser.newContext(). Creating a separate context ensures that your scraping session is isolated, preventing cookie or cache leakage between different runs.
  4. Navigate to the Target Page: Use page.goto('URL'). It is critical to wait for the page to load completely. Use the option waitUntil: 'networkidle' to ensure that any scripts responsible for populating the local storage have finished executing.
  5. Execute the Extraction Script: Since local storage exists in the browser's window object, you must use page.evaluate(). This method allows you to run a function inside the page's execution context. To get all items, you can map the localStorage object into an array of objects or a single JSON object.
  6. Handle the Returned Data: The data returned from page.evaluate() is serialized. If the local storage contains complex JSON strings, you will need to use JSON.parse() on the resulting values in your Node.js environment to work with them as objects.
  7. Close the Browser: Always call await browser.close() to prevent memory leaks and ensure the headless process terminates correctly.

Common Mistakes

  • Attempting to access localStorage outside of evaluate: A common error is trying to call page.localStorage directly in the Node.js script. Local storage is a Web API, not a Playwright API; it only exists inside the browser window.
  • Ignoring Page Load Timing: Scraping the storage immediately after goto() often results in empty data because the site's JavaScript hasn't had time to write the data to the storage.
  • Forgetting Domain Context: Local storage is bound to a specific origin (protocol, domain, and port). If you navigate to a different domain, you lose access to the storage of the previous domain.
  • Overlooking Serialization: Everything in local storage is stored as a string. Developers often forget to parse these strings back into JSON, leading to errors when trying to access nested properties.

Tips

  • Filter Specific Keys: Instead of dumping the entire storage, pass a specific key to localStorage.getItem('key_name') inside the evaluate function to reduce data overhead.
  • Use Storage State: If you need to scrape local storage across multiple pages, use browserContext.storageState(). This Playwright feature saves both cookies and local storage to a JSON file, which can be reloaded in future sessions to bypass login screens.
  • Injecting Data: You can also use page.evaluate() to set local storage values before navigating to a page, which is useful for simulating specific user states or bypassing onboarding modals.

Troubleshooting

If you are receiving an empty object or null when attempting to scrape, first check if the data is actually in Local Storage or Session Storage. They are different APIs; if it is in session storage, replace localStorage with sessionStorage in your evaluate script.

If the data only appears after a user interaction (like clicking a button), you must use page.click() and page.waitForSelector() before running the evaluation script. Some sites populate storage dynamically based on user behavior.

If you encounter a "Context destroyed" error, ensure that your await keywords are correctly placed. This usually happens when the browser closes before the evaluate function has finished returning the data.

Frequently Asked Questions

Can Playwright scrape local storage from a site that requires login?

Yes, but you must first perform the login steps using Playwright or load a pre-existing storage state file containing the authentication tokens stored in local storage.

Is there a difference between scraping local storage and cookies?

Yes. Cookies are sent with every HTTP request to the server, whereas local storage remains strictly on the client side and is never sent to the server automatically.

Does headless mode affect the ability to access local storage?

No. Headless and headed modes both have full access to the Web Storage API, as they both utilize a full browser engine.

How do I handle local storage data that is encrypted?

Playwright can only scrape the data as it exists in the browser. If the site encrypts the values before saving them to local storage, you will retrieve the encrypted string and must find the decryption logic in the site's JavaScript files.

Can I scrape local storage from multiple tabs?

Yes, as long as the tabs share the same origin (domain), they share the same local storage. You can switch between pages using browserContext.pages() and evaluate the storage on any of them.