This approach works fine for basic auth scenarios, but I run into problems when dealing with multi-factor authentication. The issue is that this method doesn’t preserve session cookies or authentication state.
I need to figure out how to capture file data while Puppeteer is running and then save it to disk. This needs to work for multiple files in a loop. Any suggestions on how to handle file downloads in Puppeteer while keeping the session active?
I’ve dealt with similar authentication issues when scraping protected content. What worked for me was using the CDP (Chrome DevTools Protocol) session to intercept network responses directly. You can enable the Network domain and listen for responseReceived events, then fetch the actual response body using Network.getResponseBody. This approach maintains all session cookies and authentication headers that Puppeteer has established. The key advantage is that you’re working within the same browser context where the authentication happened, so MFA sessions stay intact. Just make sure to filter for the specific content types you need since this will capture all network traffic.
you might wanna go with page.setRequestInterception() to catch the download request. then, use fs.writeFile() to save the response as a buffer. this lets puppeteer manage the auth, while you still store the file data before it gets downloaded.