Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] about downloads with launchPersistentContext #5947

Closed
Mbensler opened this issue Mar 25, 2021 · 8 comments
Closed

[Question] about downloads with launchPersistentContext #5947

Mbensler opened this issue Mar 25, 2021 · 8 comments
Assignees

Comments

@Mbensler
Copy link

Mbensler commented Mar 25, 2021

Hello I am writing a script in which i log in to a page, and download some files.

Here is how the download works:
I press the button with the text "Gem" which opens a target_blank page which downloads the file, and that page then closes.

<button tabindex="0" class="button ng-scope focusable" data-ng-click="save()" data-translate="" title="Gem">Gem</button>

Clicking the "Gem" button opens up the url from the following forms action

<form id="downloadForm" action="api_eb/documents/download" method="post" target="_blank" style="display: none;"> <input type="hidden" name="useMediaType" value="false" tabindex="-1"></form>

The download happens but the script freezes after the first downloaded file. Removing const dl = await download.path(); makes it so the script finishes entirely and downloads both files, BUT THE FILES ARE DELETED WHEN THE BROWSER CONTEXT IS CLOSED.

How do i solve this? Help would be much appreciated!!!

const { chromium } = require('playwright');
let { PythonShell } = require('python-shell')
const path = require('path');

const eboks = async () => {
    const pathToExtension = require('path').join(__dirname,'../extensions/nemid');
    const userDataDir = path.join(__dirname, '../extensions/nemid/userdata');
    const browser = await chromium.launchPersistentContext(userDataDir,{
        headless: false,
        slowMo: 1000,
        acceptDownloads: true,
        downloadsPath: 'C:\\Users\\mbe\\Downloads',
        args:[
            `--disable-extensions-except=${path.join(__dirname, '../extensions/nemid')}`,
            `--load-extension=${path.join(__dirname, '../extensions/nemid')}`
        ]
    });
    // Test the background page
    const backgroundPage = browser.backgroundPages()[0];

    const page = await browser.newPage();

// Download ------------------------------------------------------------------------------------------------------------>

// I AM ALREADY ON THE REQUIRED PAGE AND LOGGED IN

    const accounts = ['NEXT Forsikring A/S - Forsikringsformidling', 'PI Applications A/S - 34043027'];

    for (const account of accounts) {
        await page.click('.mailboxMenuUsers');
        await page.click(`text=${account}`);
        await page.click('text=Sparekassen');
        await page.click('text=Vælg alle');
        await page.click('a[role="link"]:has-text("Mere")');
        await page.click('text=Gem en lokal kopi');
        await page.click('text=Gem alle');

        const [ download ] = await Promise.all([
            // Start waiting for the download
            page.waitForEvent('download'),
            // Perform the action that initiates download
            page.click('button:has-text("Gem")'), // Clicking the button opens an external window that downloads the file and then closes the window.
        ]);
        // const dl = await download.path(); WITH THIS ENABLED THE SCRIPT FREEZES AFTER THE FIRST DOWNLOAD
    }

    await browser.close();
}
@Mbensler Mbensler changed the title [Question] Question about downloads [Question] about downloads Mar 25, 2021
@pavelfeldman
Copy link
Member

https://playwright.dev/docs/downloads is a good start! It comers some of your questions already.

@Mbensler Mbensler changed the title [Question] about downloads [Question] about downloads with launchPersistentContext Mar 26, 2021
@Mbensler
Copy link
Author

Mbensler commented Mar 26, 2021

https://playwright.dev/docs/downloads is a good start! It comers some of your questions already.

I changed up a lot of the code to match what was in your link but with no luck, and i also made a better description to describe my problems.

The files, however are still deleted after the script finishes, even with acceptDownloads set to true and a downloadsPath set.

Im unsure whether its because of a problem with launchPersistentContext downloads or if it's the way the download page handles downloading files, cause if i have const dl = await download.path(); after the download, it freezes the script when the first download is finished.

I have no idea how to solve this.

@aslushnikov
Copy link
Collaborator

aslushnikov commented Mar 26, 2021

@Mbensler the fact that you can't get download path is definitely a bug. A few things:

  1. Can you try minimizing your script so that the bug still reproes? For example, I see you load an extension there. Does it repro without the extension?
  2. Any chance you can share this minimal repro with us so that I can run it locally and debug it further?

@Mbensler
Copy link
Author

Mbensler commented Mar 29, 2021

@Mbensler the fact that you can't get download path is definitely a bug. A few things:

  1. Can you try minimizing your script so that the bug still reproes? For example, I see you load an extension there. Does it repro without the extension?
  2. Any chance you can share this minimal repro with us so that I can run it locally and debug it further?

Hi @aslushnikov

  • The extension is needed to login to the site, so it's impossible to get to the download/s without logging in.
  • I don't think there's a way i can share it so you will be able to run it yourself. The problem is that the website I'm logging in to is a Danish official government website, that requires you to load specific files from your PC, to be able to log in (It's a system most of scandinavia uses to log in to government stuff) called NemId. The extension im loading is a chrome extension with the same name.

Basically, from what i can gather the error happens when pressing the save button. The save button opens a target=_blank page that contains the download, but while it is downloading the script just keeps running in the background, and if const dl = await download.path(); isn't outcommented the script just freezes after pressing the save/download button.

It seems like it doesn't recognize downloads that open in other windows?

@klhex
Copy link
Contributor

klhex commented Mar 29, 2021

@Mbensler Regarding the freezing of your test when using download.<...>() functions, I have experienced the same issue here: #5962

I guess it might also work on your side (= avoiding the test to freeze when using download.functions) if you either use Firefox in headful mode instead of Chromium or you use the headless mode for Chromium. (As a temporary workaround if the described issue gets resolved at some point.)

@yury-s
Copy link
Member

yury-s commented Apr 8, 2021

I suspect it has the same root cause as #5962 and should be fixed by #6082. We are waiting for the next Chromium Dev build that includes the upstream fix.

@yury-s
Copy link
Member

yury-s commented Jun 4, 2021

Should be fixed by #6082, can you validate the fix with playwright@next?

@yury-s yury-s closed this as completed Jun 4, 2021
@Mbensler
Copy link
Author

Mbensler commented Jun 7, 2021

Should be fixed by #6082, can you validate the fix with playwright@next?

Fixed, validated with playwright@next.
Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants