【发布时间】:2021-09-12 14:26:21
【问题描述】:
我想模拟使用 puppeteer 的下载功能,并且 puppeteer 脚本在云函数中运行。我触发了下载按钮并将下载路径设置为 /tmp/ 但是当我从 /tmp 读取文件时,我下载的文件没有显示在那里。有时它会随机显示在那里。
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(0);
await page.goto(url);
const downloadPath = `${os.tmpdir()}` + '/';
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath
});
await page.reload({ waitUntil: 'networkidle0' });
const filename = `test-report.pdf`;
await page.click('#screenshot'); //simulate download
await page.waitForTimeout(4000);
const files = fs.readdirSync(downloadPath);
console.log('Dir files ', files);
const filePath = downloadPath + filename;
const content = fs.readFileSync(filePath).toString("base64");
await page.close();
这个错误来了:
在 /tmp/report.pdf 中找不到文件。
如何解决?
puppeteer 版本为 ^10.0.0,Node js 版本为 12
【问题讨论】:
标签: node.js google-cloud-functions pdf-generation puppeteer screenshot