How to Clear History (Clear browsing data) In Node.js Puppeteer Headless=false Chromium browser
It is not possible to navigate to the browser settings pages (chrome://...
) like that.
You have three options:
- Use an incognito window (called context in puppeteer)
- Use a command from the Chrome DevTools Protocol to clear history.
- Restart the browser
Option 1: Use an incognito window
To clear the history (including cookies and any data), you can use an "incognito" window called BrowserContext in puppeteer.
You create a context by calling browser.createIncognitoBrowserContext()
. Quote from the docs:
Creates a new incognito browser context. This won't share cookies/cache with other browser contexts.
Example
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
// Execute your code
await page.goto('...');
// ...
await context.close(); // clear history
This example will create a new incognito browser window and open a page inside. From there on you can use the page
handle like you normally would.
To clear any cookies or history inside, simply close the context via context.close()
.
Option 2: Use the Chrome DevTools Protocol to clear history
If you cannot rely on using context (as they are not supported when using extensions), you can use the Chrome DevTools Protocol to clear the history of the browser. It has functions which are not implemented in puppeteer to reset cookies and cache. You can directly use functions from the Chrome DevTools Protocol by using a CDPSession.
Example
const client = await page.target().createCDPSession();
await client.send('Network.clearBrowserCookies');
await client.send('Network.clearBrowserCache');
This will instruct the browser to clear the cookies and cache by directly calling Network.clearBrowserCookies
and Network.clearBrowserCache
.
Option 3: Restart the browser
If both approaches are not feasible, you can alway restart the browser, by closing the old instance and creating a new one. This will clear any stored data.
Zilvia Smith
Updated on August 06, 2020Comments
-
Zilvia Smith almost 4 years
I am tring to delete history in headless=false browser with node.js puppeteer through below code but non of method work.
await page.goto('chrome://settings/clearBrowserData'); await page.keyboard.down('Enter');
Second code
await page.keyboard.down('ControlLeft'); await page.keyboard.down('ShiftLeft'); await page.keyboard.down('Delete'); await page.keyboard.down('Enter');
i tried to used
.evaluateHandle()
and.click()
function too but non of them work. if anyone know how to clear history with puppeteer please answer me. -
Zilvia Smith about 5 yearsThanks for reply and solution. but Incognito Browser not support chrome extension.
-
Thomas Dondorf about 5 years@ZilviaSmith Is it an option to restart the browser?
-
Zilvia Smith about 5 yearsYes. restart is option but some time we need to do clear history & this is important part for puppeteer and chromium.
-
Thomas Dondorf about 5 years@ZilviaSmith I added a second option which shows how to clear browser cache and cookies :)
-
Zilvia Smith about 5 yearsNot work for clear history. page.deleteCookie(); and page.setCacheEnabled(false); is best option then use dev tool.
-
Thomas Dondorf about 5 years@ZilviaSmith What exactly do you mean by history then? Cookies? Cache?
-
Zilvia Smith about 5 yearsi want to Clear browsing data ( Browsing history, download history, cookies, cache, password, fill data) (chrome://settings/clearBrowserData) => advance tab
-
Thomas Dondorf about 5 years@ZilviaSmith The code from my answer should do exactly that (download history, password fill, etc. is not present in headless mode). What is not working then?
-
Zilvia Smith about 5 yearsi am using headless=false.
-
Thomas Dondorf about 5 years@ZilviaSmith Sorry, forgot that.. In that case you can only do a full reset by closing the old window and relaunching (added the option to the answer).