Skip to content

[Question] about downloads with launchPersistentContext #5947

@Mbensler

Description

@Mbensler

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions