microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
65.6k stars 3.57k forks source link

[Question] Download starts in new tab #3689

Closed osmenia closed 4 years ago

osmenia commented 4 years ago

Hi Team,

there is a download button, after pressing the button a new tab opens and the download starts. After the download is completed, new tab closes. In the chrome browser I see that the file has been downloaded, but const path = await download.path (); shows nothing. VS Code does not show any error.

 const [download] = await Promise.all([
      page.waitForEvent("download"), // wait for download to start
      page.click("span.icon-am-download"),
    ]);

    const path = await download.path();

I think the problem is: A new tab opens, page.waitForEvent ("download") cannot catch the Download event because the event download is started in new tab.

How can a file be downloaded if the download starts in a new tab?

This is HTML Code: `

    </div>`

Thank you very much

pavelfeldman commented 4 years ago

Depending on how this download is implemented (via script, redirect or something else), you could try waiting for the popup first via waitForEvent("popup") and then waiting for the download on that new popup...

osmenia commented 4 years ago

Depending on how this download is implemented (via script, redirect or something else), you could try waiting for the popup first via waitForEvent("popup") and then waiting for the download on that new popup...

Thanks a lot for your answer!

I found one example page with popup and download exe file. can you please show me how to download file inside popup.

Hier is code:

const { chromium } = require("playwright");

(async () => {
  const browser = await chromium.launch({ headless: false });
  const crContext = await browser.newContext({ acceptDownloads: true });
  const page = await crContext.newPage();
  await page.goto("https://webshop.vda.de/QMC/de/ebooks");

  const [download] = await Promise.all([
    page.waitForEvent("download"), // wait for download to start
    page.click("#ph-topic > div.page-body > p > a:nth-child(16) > strong"),   //vda_qmc_eReader.exe
  ]);

  const path = await download.path();

  console.log(path);

  await page.close();
  await browser.close();
})();
osmenia commented 4 years ago

i tried this, does not work, because page close after download finished - Error: page.waitForEvent: Page closed

 const [download] = await Promise.all([
      ([popup1] = await Promise.all([
        page.waitForEvent("popup"),
        page.click("#ph-topic > div.page-body > p > a:nth-child(16) > strong"),
      ])),
      popup1.waitForEvent("download"), // wait for download to start
    ]);

    console.log(download);
ingvar-nikiforov commented 4 years ago

@osmenia your code works in Chromium headless.

There is a TODO in code for Chromium headful: https://github.com/microsoft/playwright/blob/8df1fe47bcc34f0d398f17a37f7a98eabd35b2f5/test/download.spec.ts#L280-L284

osmenia commented 4 years ago

@ingvar-nikiforov thanks a lot for your info. wow, i did not try in headless mode,