16

I need to take with Puppeteer all the data that a website saves: cookies and localStorage (for example after Login). I have read all Puppeteer documentation but I can not find anything about localStorage.

enter image description here

I can get cookies but I don't know to get localStorage. For example:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://www.twitter.com/')

  //
  // code for login
  //

  const returnedCookie = await page.cookies();  
  console.log(returnedCookie)
  // const localStorage = ??
  // console.log(localStorage)

  await browser.close()
})()
vsemozhebuty
  • 12,992
  • 1
  • 26
  • 26
kurtko
  • 1,978
  • 4
  • 30
  • 47

4 Answers4

18

Easier way that worked for me:

const localStorage = await page.evaluate(() =>  Object.assign({}, window.localStorage));
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Guido Dizioli
  • 2,007
  • 2
  • 17
  • 29
12

I found the way:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://www.twitter.com/')

  //
  // code for login
  //

  const returnedCookie = await page.cookies();  
  console.log(returnedCookie)

  await page.waitFor( 10000 );
  const localStorageData = await page.evaluate(() => {
    let json = {};
    for (let i = 0; i < localStorage.length; i++) {
      const key = localStorage.key(i);
      json[key] = localStorage.getItem(key);
    }
    return json;
  });

  console.log(localStorageData)

  await browser.close()
})()
kurtko
  • 1,978
  • 4
  • 30
  • 47
1

Guido's answer works but if you want to return just a single value (and not the whole Local/Session storage object) from local/session storage, then you can use:

const localStorage = await page.evaluate(() => localStorage.getItem("myKey"));

Note: If it's empty, it will return null.

Puppeteer 5.3.1.

Mikulas
  • 11
  • 2
0

my solution is:

const puppeteer = require("puppeteer");
const fs = require("fs");

async function saveData() {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();

  // go to example.com
  await page.goto("https://example.com");

  // Wait for the user to log in and the data to populate
  await page.waitForTimeout(30000);

  // Get localStorage data
  const localStorageData = await page.evaluate(() => {
    const data = {};
    for (let i = 0; i < localStorage.length; i++) {
      const key = localStorage.key(i);
      const value = localStorage.getItem(key);
      data[key] = value;
    }
    return data;
  });

  // Store the localStorage data in a file
  const localStorageDataJSON = JSON.stringify(localStorageData);
  fs.writeFileSync("localStorageData.json", localStorageDataJSON, "utf8");

  await browser.close();
}

saveData();
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 04 '23 at 10:19