You can clear cookies and cache in Puppeteer by using the page.evaluate
method to execute JavaScript code inside the browser context. Here's an example of how you can clear cookies and cache using Puppeteer:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Clear cookies
await page.evaluate(() => {
document.cookie.split(';').forEach(function(cookie) {
document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
});
});
// Clear cache
await page.evaluate(() => {
return caches.keys().then(function(keys) {
keys.forEach(function(key) {
return caches.delete(key);
});
});
});
await browser.close();
})();
In this example, we use the document.cookie
property to iterate through all cookies and clear them by setting their expiration date to the past. We also use the caches.keys()
method to iterate through all cache keys and delete each cache entry.
Note that clearing cookies and cache in Puppeteer using this method may not work for all websites, as some websites may have additional security measures in place to prevent cookie and cache manipulation.