1

Currently I'm getting an issue where Puppeteer crashes upon using the setCookies method. I'm currently using Puppeteer v 1.4.0 (latest version as of writing this) as well as the Chromium version that is bundled with Puppeteer, and here is the code that is giving me trouble:

const puppeteer = require('puppeteer');
const moment = require('moment');
(async () => {
  const browser = await puppeteer.launch(
    {
      headless: false
    }
  );
  const page = await browser.newPage();
  await page.goto('https://google.com');
  const currentUrl = await page.url();
  await browser.close();
  const browser1 = await puppeteer.launch(
    {
      headless: false
    }
  );
  const page1 = await browser1.newPage();
  const cookie = await currentUrl.split("/");
  await page1.setCookie({
    'name': 'samplename',
    'value': cookie[0],
    'domain': 'sampledomain',
    'path': cookie[0] + '/' + cookie[0] + '/' + cookie[0],
    'expires': moment().add(21, 'days').valueOf(),
    'httpOnly': false,
    'secure': true,
    'sameSite': "Lax"
  });
  await page1.goto(currentUrl);
})();

and here is the error message

(node:64704) UnhandledPromiseRejectionWarning: Error: Protocol error (Network.setCookies): Target closed.
    at Promise (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Connection.js:200:56)
    at new Promise (<anonymous>)
    at CDPSession.send (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Connection.js:199:12)
    at Page.setCookie (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Page.js:320:26)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)
(node:64704) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:64704) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I've looked into the issue on my own for a while, and multiple sources seem to say that not actually executing async is the problem, however, I believe I am running everything in async (however, as this is my first time doing anything async on NodeJS, I could be making critical errors in judgement). I've tried verifying my Chromium and uninstalling + reinstalling Puppeteer, but nothing seems to avail.

Stephen
  • 306
  • 1
  • 3
  • 10

1 Answers1

1

The reason you're gettning an error is in this line:

'path': cookie[0] + '/' + cookie[0] + '/' + cookie[0],

It resolves into https:/https:/https: which is not a valid value for this property.

Try setting path to '/' or just do not set this property and it's going to work just fine.

You can find more reading on how to use path here and here.

Antonio Narkevich
  • 3,736
  • 15
  • 28