puppeteer: [Bug]: new-headless mode downloads files
Bug expectation
In old-headless mode, if you page.goto()
a binary file (e.g. docx/xlsx/etc) then the navigation is aborted and that’s the end of it. In new-headless mode then it claims to abort it, but a visible download window appears (i.e. it fails to be “headless”) and the file is downloaded to the Downloads folder.
Bug behavior
- Flaky
Minimal, reproducible example
import puppeteer from 'puppeteer'
const browser = await puppeteer.launch({ headless: 'new' })
const context = await browser.createIncognitoBrowserContext()
const page = await context.newPage()
try {
await page.goto('https://unequivocal.eu/dl/example.docx')
} catch (err) {
console.log(err)
// need a delay because otherwise the browser exits before the download completes
await new Promise(resolve => setTimeout(resolve, 5000))
}
await browser.close()
Error string
no error
Puppeteer configuration
No response
Puppeteer version
20.1.2
Node version
16.14.0
Package manager
npm
Package manager version
9.4.0
Operating system
Windows
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 15
I have been unable to get any variation of setDownloadBehavior to work in newer puppeteer. Previously working code using Page.setDownloadBehavior fails, as does goto method above. Based on https://github.com/puppeteer/puppeteer/issues/3722#issuecomment-679509973 I have a working function that downloads a url to a local file by using fetch in the page context and marshaling the results to the node context.
Usage:
The main downsides I see are:
But it works for my purposes and I hope it helps others.