Rayrun
← Back to Discord Forum

Rename file to custom name after download

Hello! I have recently been having a lot of trouble renaming my file to end with '.csv'. I have to download four different files, then rename each. I am automating a google docs like app which demands that my files end with 'csv'. However when I use suggestedFilename(), it names all of the downloads the same name which ends up getting the files confused even when they are saved to different variables. I tried using an object literal to run something like: const filePath4 = 4${download4.suggestedFilename()} This doesn't work either and it keeps giving me the error that this path doesnt exist. Does anyone know how I could rename each a unique name that also ends with 'csv'? Thanks for any help.

This thread is trying to answer question "How can I rename each downloaded file with a unique name that also ends with 'csv'?"

0 replies

Answer

Saving Downloaded Files with Unique Names in Playwright

You can use the saveAs method from Playwright's Download class to rename each downloaded file with a unique name ending in '.csv'. Here's how:

First, start waiting for the download event before clicking the download button.

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

Once the download event is triggered, get a reference to the Download object.

const download = await downloadPromise;

Next, generate a unique name for the downloaded file. Let's assume you have a function generateUniqueName() for this purpose.

const uniqueName = generateUniqueName() + '.csv';

Then, specify the path where the download should be saved.

const savePath = 'https://ray.run/path/to/save/download/' + uniqueName;

Finally, save the downloaded file with the specified path and name.

await download.saveAs(savePath);

And that's it! You've successfully renamed a downloaded file with a unique name ending in '.csv' using Playwright. Remember to always refer to the official Playwright documentation for more detailed information.

TwitterGitHubLinkedIn
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 [email protected].