【问题标题】:How to catch a download with playwright?如何与剧作家一起下载?
【发布时间】: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();
    })();

Full file here

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事件,你可以使用节点的EventEmitterPage类扩展,例如page.on('downloadWillBegin', doSomething);

标签: node.js playwright


【解决方案1】:

令人尴尬的是,我在下载开始之前就关闭了浏览器。

原来下载错误是由client部分引起的。但是,这意味着我无法控制文件的保存位置。

headless: false 时下载有效,headless: true 时无效。

如果有人有更好的答案,那就太好了!

【讨论】:

    【解决方案2】:

    您可以使用waitForTimeout

    我试过{headless: true} & await page.waitForTimeout(1000);

    它工作正常。你可以检查相同的here

    【讨论】:

      【解决方案3】:

      要下载文件(也是它的缓冲区),我强烈推荐这个模块:Got node module。它更容易、更干净、更轻便。

      (async () => {
      const response = await got('https://sindresorhus.com')
          .on('downloadProgress', progress => {
              // Report download progress
          })
          .on('uploadProgress', progress => {
              // Report upload progress
          });
      
          console.log(response);
      })();
      

      【讨论】:

        【解决方案4】:

        看看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());
        

        【讨论】:

          猜你喜欢
          • 2023-02-26
          • 2022-01-15
          • 2022-10-06
          • 2022-11-09
          • 2022-12-15
          • 2021-01-01
          • 1970-01-01
          • 2022-06-13
          • 2022-01-01
          相关资源
          最近更新 更多