How can I remove browsing history in a non-headless Chromium browser using Node.js and Puppeteer?

I’m attempting to clear the browsing history in a non-headless Chromium browser using Node.js with Puppeteer. I’ve implemented the following snippets, but none have yielded results.

await page.goto('chrome://settings/clearBrowserData');
await page.keyboard.press('Enter');

For my second attempt:

await page.keyboard.press('Control');
await page.keyboard.press('Shift');
await page.keyboard.press('Delete');
await page.keyboard.press('Enter');

I also tried using .evaluateHandle() and .click() methods, but those didn’t work either. If anyone has a solution for clearing history in Puppeteer, I would appreciate your guidance.

To remove browsing history in Chromium when using Puppeteer, the direct approach you’re attempting might be blocked by browser security restrictions. Instead, you can launch the browser with a fresh user data directory to simulate a clean state each time you run your script. You can achieve this by specifying the --user-data-dir argument with a temporary directory, like this:

const browser = await puppeteer.launch({
  headless: false, 
  args: ['--user-data-dir=/tmp/my-temp-dir'], // Use unique path per session
});

Each session starts without any history, effectively clearing it. Remember to delete the temporary data folder between sessions.