Cookies are small pieces of data stored on the user's computer by the web browser while browsing a website. They hold a modest amount of data specific to a particular client and website and can be accessed either by the web server or the client computer.
Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default but can be configured to run full (non-headless) Chrome or Chromium.
Handling cookies in Puppeteer is straightforward. You can set, delete, and get cookies with the functions page.setCookie()
, page.deleteCookie()
, and page.cookies()
respectively.
Here is an example in JavaScript on how to handle cookies in Puppeteer:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Navigate to a website
await page.goto('http://example.com');
// Set a cookie
await page.setCookie({name: 'test', value: '123'});
// Get cookies
let cookies = await page.cookies();
console.log(cookies);
// Delete a cookie
await page.deleteCookie({name: 'test'});
cookies = await page.cookies();
console.log(cookies);
await browser.close();
})();
In the above example:
- We first navigate to the website where we want to handle cookies.
- We set a cookie with the name 'test' and the value '123'.
- We then get all cookies and print them to the console.
- We delete the cookie with the name 'test'.
- Finally, we get all cookies again and print them to the console. The cookie 'test' we set earlier should now be gone.
This is a basic example of how you can handle cookies in Puppeteer. Depending on the website, you might have to handle session cookies, secure cookies, or httpOnly cookies, which would require a more advanced setup.