ps0*_*604 8 javascript node.js google-chrome-headless puppeteer
我有一个显示页面的应用程序,用户单击一个按钮,然后下载一个 CSV 文件。我想用 Puppeteer 运行它。
问题是 CSV 下载为空且有错误。headless真假都会发生这种情况。页面加载完成,我增加了超时时间,但它仍然失败。可能是什么问题?
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.goto('http://localhost:4400/login', { waitUntil: 'networkidle2' });
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: './',
});
await page.waitForSelector('#run-and-export');
await page.click('#run-and-export');
// file-downloaded is turned on when the file finished downloading (not to close the window)
await page.waitForSelector('#file-downloaded', { timeout: 120000 });
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
应用程序中生成要下载的文件的代码是一个 Angular 服务:
@Injectable({
providedIn: 'root'
})
export class DownloadService {
downloadFile(content:any, fileName: string, mimeType: string){
var blob = new Blob([(content)], {type: mimeType});
var a = document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = fileName;
a.click();
}
}
Run Code Online (Sandbox Code Playgroud)
这就是使这项工作的原因:
const downloadPath = path.resolve('/my/path');
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: downloadPath
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1724 次 |
| 最近记录: |