Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file with Playwright

Tags:

playwright

How to download a file with Playwright?

I'm aware of this question How to catch a download with playwright? but that example code does not work. Using the latest released Playwright, there is no 'pageTarget' function on the browser instance:

const client = await browser.pageTarget(page).createCDPSession();
like image 581
Don Box Avatar asked Nov 02 '25 09:11

Don Box


2 Answers

All the downloaded files belonging to the browser context are deleted when the browser context is closed. All downloaded files are deleted when the browser closes.

Download event is emitted once the download starts. Download path becomes available once download completes:

const [ download ] = await Promise.all([
  page.waitForEvent('download'), // wait for download to start
  page.click('a')
]);
// wait for download to complete
const path = await download.path();
...

https://github.com/microsoft/playwright/blob/master/docs/api.md#class-download

like image 170
user1862965 Avatar answered Nov 05 '25 16:11

user1862965


Playwright is going to support downloads in a cross-browser compatible way soon, you can track this feature request.

For now the above Chromium-specific snippet could be fixed by changing the line to:

const client = await context.newCDPSession(page);

which uses the new method for creating CDP sessions.

like image 36
Yury Semikhatsky Avatar answered Nov 05 '25 15:11

Yury Semikhatsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!