What is the method to save and rename downloaded files in Playwright?


Sure, you can rename your downloaded files using Playwright. It's as easy as using the saveAs() method from the Download object. This method lets you specify a custom path where the download should be copied. By changing the file name in the path, you can rename the downloaded file.

Here's a quick example:

const downloadPromise = page.waitForEvent('download');
await page.getByText('Download file').click();
const download = await downloadPromise;

// Specify a custom path with a different file name
await download.saveAs('/path/to/save/custom_name.txt');

In this code, we start the download by clicking on an element with the text "Download file". We then wait for the 'download' event using waitForEvent(). Once the event is triggered, we get the Download object (download). We then use saveAs() to specify a custom path (/path/to/save/) and a new file name (custom_name.txt). The downloaded file will be saved with this new name at the specified location.

Remember to replace /path/to/save/ with your desired directory path and "custom_name.txt" with your preferred custom name for each downloaded file.

And that's it! You've just renamed your downloaded files using Playwright.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.