To upload a PDF file without a submit button using setInputFiles
in @playwright/test, first locate the input element with the type "file" using the page.getByLabel()
method. Then, call setInputFiles()
on the input element, passing either a string or an array of strings representing the file paths.
Here's an example for selecting one file:
await page.getByLabel('Upload file').setInputFiles('myfile.pdf');
For multiple files, pass an array of strings:
await page.getByLabel('Upload files').setInputFiles(['file1.txt', 'file2.txt']);
To deselect all files, pass an empty array:
await page.getByLabel('Upload file').setInputFiles([]);
You can also upload a buffer from memory:
await page.getByLabel('Upload file').setInputFiles({
name: 'file.txt',
mimeType: 'text/plain',
buffer: Buffer.from('this is test')
});
If the input element is created dynamically, handle the page.on('filechooser')
event or use a corresponding waiting method upon your action. Start waiting for this event before clicking on any button that triggers it. Once triggered, handle it by calling setFiles()
on its returned object.
If you still have questions, please ask a question and I will try to answer it.
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].