如何使用剧作家下载生成的pdf文件?

问题描述 投票:0回答:1

我正在尝试使用 playwright 下载文件,生成此文件意味着 url 不以“.pdf”结尾,并且在 url 中强制此链接不会开始下载。我能够导航直到生成 pdf,但现在如何下载它?

这是我的代码:

const {chromium} = require('playwright');
const path = require('path');

(async () => {
    const browser = await chromium.launch({
        headless: true,
    });
    const context = await browser.newContext({
        acceptDownloads: true
    });
    const page = await context.newPage();

    const downloadPath = path.join(__dirname, 'edt');

    await page.goto('https://planif.eppe.ui/jsp/custom/eppee/easyMyPlanning.jsp');
    await page.getByLabel('Identifiant :').click();
    await page.getByLabel('Identifiant :').fill('myusername');
    await page.getByLabel('Identifiant :').press('Tab');
    await page.getByLabel('Mot de passe :').fill('mysecretpassword');
    await page.getByLabel('Mot de passe :').press('Enter');
    await page.getByRole('combobox').selectOption('10');
    await page.getByRole('button', { name: 'Submit' }).click();
    await page.locator('.footer > a').click();
    const page1Promise = page.waitForEvent('popup');
    await page.frameLocator('frame[name="link"]').getByRole('link', { name: 'Export PDF...' }).click();
    const page1 = await page1Promise;
    const downloadPromise = page1.waitForEvent('download');
    await page1.getByRole('button', { name: 'Submit' }).click();
    const download = await downloadPromise;
    await download.saveAs(path.join(downloadPath, 'edt.pdf'));
    await browser.close();
})();

使用此代码,我最终让浏览器等待我尝试下载文件,我怎样才能自动执行此操作?

ps:使用此代码并且我的浏览器处于非无头模式,如果我手动按下下载按钮,我最终会到达此页面结束 enter image description here

javascript node.js playwright
1个回答
0
投票

你解决了吗?我也遇到过这样的场景。

© www.soinside.com 2019 - 2024. All rights reserved.