node.js - 如何 catch 与编剧的下载?

我正在尝试使用 Playwright 从网站下载文件.触发下载的按钮执行一些 js 然后开始下载。

使用 .click 函数单击按钮会触发下载,但会显示错误:失败 - 下载错误

我试过使用 devtools 协议(protocol) 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();
    })();

Full file here

有一个proposal for a better download API Playwright , 但我找不到当前的 API。 有人建议 something to do with the downloadWillBegin event会很有用,但我不知道如何从 Playwright 访问它。

我对应该改用 Puppeteer 的建议持开放态度,但我转向了剧作家,因为我也不知道如何使用 Puppeteer 下载文件,并且 the issue related to it suggested that the whole team had moved to Playwright .

最佳答案

看看 page.on("download")

const browser = await playwright.chromium.launch({});
const context = await browser.newContext({ acceptDownloads: true });
const page = await context.newPage();
await page.goto("https://somedownloadpage.weburl");
await page.type("#password", password);
await page.click("text=Continue");
const download = await page.waitForEvent("download");
console.log("file downloaded to", await download.path());

https://stackoverflow.com/questions/60501133/

相关文章:

c# - 如何修复错误 MC1000 未知构建错误, 'Could not find type '

java - Hibernate NameGenerator 中带有 hql 查询的 Nullpoi

android - Android下载管理器如何修改更新间隔

amazon-web-services - 无效的 LambdaFunctionAssociatio

php - html2pdf: 生成一个空白页

javascript - Firebase:使用预留主机 URL 时未创建 Firebase 应用程

graphql - 如何在自定义指令上触发 visitInputObject 方法?

php - 找不到类放大器

amazon-web-services - 尝试使用 moto 模拟 s3 时出现 NoSuchBu

python - 如何通过 Python 使用 GeckoDriver 和 Firefox 使 Se