OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

how to download a generated pdf file using playwright?

  • Thread starter Thread starter lolozen
  • Start date Start date
L

lolozen

Guest
I'm trying to download a file using playwright, this file is generated which means the url doesn't ends with a '.pdf' and forcing this link in the url doesn't start the download. I was abble to navigate until the pdf is generated but how can I download it now ?

here is my code:

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

with this code I end up having the browser waiting for me to try to download the file how can I automate this ?

ps: with this code and with my browser in non headless mode I end up on this page end if I manualy press the download button it works enter image description here

<p>I'm trying to download a file using playwright, this file is generated which means the url doesn't ends with a '.pdf' and forcing this link in the url doesn't start the download. I was abble to navigate until the pdf is generated but how can I download it now ?</p>
<p>here is my code:</p>
<pre class="lang-js prettyprint-override"><code>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();
})();

</code></pre>
<p>with this code I end up having the browser waiting for me to try to download the file how can I automate this ?</p>
<p>ps: with this code and with my browser in non headless mode I end up on this page end if I manualy press the download button it works
<a href="https://i.sstatic.net/mSMng.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/mSMng.png" alt="enter image description here" /></a></p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top