Puppeteer is a Node.js library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It's used for web scraping, generating screenshots and PDFs of web pages, automating form submissions, and more.
With Puppeteer, you can handle browser context using the BrowserContext
class. A browser context represents an isolated environment, similar to incognito mode inside which a single session operates. Cookies/plug-ins are shared among browsing contexts.
If you want to start a new context, you can use the browser.createIncognitoBrowserContext()
function. Here's a JavaScript example:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
await page.goto('https://example.com');
// ... perform operations in the new context
})();
In this example, a new incognito browser context is created, and a new page is opened inside this context.
You can close the context once you're done with it using the BrowserContext.close()
method:
await context.close();
Remember that when you launch a new browser, Puppeteer automatically creates a BrowserContext
for you, and all operations (like creating new pages) happen within this default context. You can access this context with browser.defaultBrowserContext()
.
If you want to clear the browser context (for example, to clear cookies), you can use the BrowserContext.clearCookies()
method:
await context.clearCookies();
This way, you can manipulate and control browser contexts using Puppeteer. This is especially useful in web scraping where you might want to isolate different scraping tasks or simulate separate user sessions.