How to login using netscape cookies(not json) in puppeteer?

51 views Asked by At

I'm working on automating a login process using Puppeteer for a website that utilizes Netscape cookies for authentication. I've obtained the necessary Netscape cookies from a previous session and now I want to use them to log in programmatically. However, I'm encountering some difficulties in implementing this process.

Could someone provide guidance or a code example on how to set Netscape cookies in Puppeteer and use them to authenticate a user session? Specifically, I need assistance with the steps to properly set the cookies and navigate through the login flow using Puppeteer.

This code is for json cookies working fine but i want to login using txt netscape cookies

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

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

try {
// Read cookies from file (assuming cookies.json contains an array of cookie objects)
const cookiesString = fs.readFileSync('C:\\Users\\myuser\\Desktop\\webscraper\\cookies.json');
const cookies = JSON.parse(cookiesString);

// Set each cookie individually
for (const cookie of cookies) {
await page.setCookie(cookie);
}

await page.goto('https://example.com');
} catch (error) {
console.error('Error setting cookies:', error);
} finally {
// Close the browser when done
// await browser.close();
  }
})();

I've tried setting the cookies using the page.setCookie() method and then navigating to the login page but its not working. I want to log in with Netscape cookies

Error: ProtocolError: Protocol error (Network.setCookies): Invalid cookie fields

0

There are 0 answers