【发布时间】:2020-06-15 11:23:06
【问题描述】:
我正在尝试使用Playwright 从网站下载文件。触发下载的按钮做一些js然后开始下载。
使用.click 函数单击按钮会触发下载,但会显示错误:失败 - 下载错误。
我尝试过使用 devtools 协议 Page.setDownloadBehavior,但这似乎没有任何作用。
const playwright = require("playwright");
const { /*chromium,*/ devices } = require("playwright");
const iPhone = devices["iPad (gen 7) landscape"];
(async () => {
const my_chromium = playwright["chromium"];
const browser = await my_chromium.launch({ headless: false });
const context = await browser.newContext({
viewport: iPhone.viewport,
userAgent: iPhone.userAgent
});
const page = await context.newPage();
const client = await browser.pageTarget(page).createCDPSession();
console.log(client);
await client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath: "C:/in"
});
//...and so on
await page.click("#download-button");
browser.close();
})();
Playwright中有一个proposal for a better download api,但是我找不到当前的API。
有人建议 something to do with the downloadWillBegin event 会很有用,但我不知道如何从 Playwright 访问它。
我对我应该使用 Puppeteer 的建议持开放态度,但我转向了剧作家,因为我也不知道如何使用 Pupeteer 下载文件,以及the issue related to it suggested that the whole team had moved to Playwright。
【问题讨论】:
-
关于
downloadWillBegin事件,你可以使用节点的EventEmitter,Page类扩展,例如page.on('downloadWillBegin', doSomething);
标签: node.js playwright