我正在尝试使用 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();
})();
使用此代码,我最终让浏览器等待我尝试下载文件,我怎样才能自动执行此操作?
你解决了吗?我也遇到过这样的场景。