Can the browser turned headless mid-execution when it was started normally, or vice-versa?

Short answer: It’s not possible

Chrome only allows to either start the browser in headless or non-headless mode. You have to specify it when you launch the browser and it is not possible to switch during runtime.

What is possible, is to launch a second browser and reuse cookies (and any other data) from the first browser.

Long answer

You would assume that you could just reuse the data directory when calling puppeteer.launch, but this is currently not possible due to multiple bugs (#1268, #1270 in the puppeteer repo).

So the best approach is to save any cookies or local storage data that you need to share between the browser instances and restore the data when you launch the browser. You then visit the website a second time. Be aware that any state the website has in terms of JavaScript variable, will be lost when you recrawl the page.

Process

Summing up, the whole process should look like this (or vice versa for headless to headfull):

  • Crawl in non-headless mode until you want to switch mode
  • Serialize cookies
  • Launch or reuse second browser (in headless mode)
  • Restore cookies
  • Revisit page
  • Continue crawling

Leave a Comment