When automating web applications with Playwright in Java, file upload and download scenarios are among the most common test cases you will encounter. From attaching a profile picture to downloading a PDF report, understanding how to handle file interactions in Playwright ensures your automation covers real-world workflows.
In this article, we’ll cover:
Throughout this article, we will use our testing playground website – https://testkru.com/Elements/Files, to demonstrate how to upload and download files

Uploading Files in Playwright Java
For uploads, Playwright doesn’t interact with the OS file picker. Instead, it directly sets the file(s) to an <input type=”file”> element in the DOM. This avoids flaky OS dialogs and works in headless mode too.
We’ll cover how to upload a single file, upload multiple files, and clear uploaded files.
Upload a single file
We will try to upload this jpg file to the highlighted element on this page – https://testkru.com/Elements/Files.

So, what we need to do to upload the file?
First, we need to locate the element. As shown in the image above, the element has the ID “singleFileUpload“, which we can use to find it.
Locator singleFileUploadLocator = page.locator("#singleFileUpload");
Next, we will use the “setInputFiles” method to provide the path of the file we want to upload.
singleFileUploadLocator.setInputFiles(Paths.get("path_of_the_file"));
Here’s our project structure: the test class file ( CodekruTest ) is located under src/main/java, while the file we want to upload ( plant_image.jpg ) is stored under src/main/resources.


CodekruTest file code
import com.microsoft.playwright.*;
import java.nio.file.Paths;
public class CodekruTest {
public static void main(String[] args) {
// Initialize the Playwright library (required to use Playwright APIs)
Playwright playwright = Playwright.create();
// 1. Launch the Chromium browser instance
// setHeadless(false) → Launches the browser in a visible UI mode (not headless)
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
// 2. Create a new browser context
BrowserContext context = browser.newContext();
// 3. Open a new page (tab) inside the created browser context
Page page = context.newPage();
// 4. Navigate to the target website
// This URL contains a file upload element that we will interact with
page.navigate("https://testkru.com/Elements/Files");
// 5. Locate the single file upload input element
Locator singleFileUploadLocator = page.locator("#singleFileUpload");
// 6. Upload a file to the located <input type="file"> element
// Paths.get(...) → Creates a file path reference to the file in 'src/main/resources'
// setInputFiles(...) → Directly sets the file for the input without triggering the OS file dialog
singleFileUploadLocator.setInputFiles(Paths.get("src/main/resources/plant_image.jpg"));
}
}
When we run the above code, it will upload the “plant_image.jpg” file, as shown in the image below.


Upload multiple files
Now, let’s try uploading multiple files to the highlighted element shown below.


Most of the code will stay the same as before. We just need to make two changes:
1) Change the element’s id to “multiFileUpload”
Locator multiFileUploadLocator = page.locator("#multiFileUpload");
2) The setInputFiles method also accepts a Path[] array, allowing us to pass the paths of multiple files, as shown below.
multiFileUploadLocator.setInputFiles( new Path[] {
Paths.get("src/main/resources/plant_image.jpg"),
Paths.get("src/main/resources/codekru.png")
});
Whole code
import com.microsoft.playwright.*;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CodekruTest {
public static void main(String[] args) {
// Initialize the Playwright library (required to use Playwright APIs)
Playwright playwright = Playwright.create();
// 1. Launch the Chromium browser instance
// setHeadless(false) → Launches the browser in a visible UI mode (not headless)
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
// 2. Create a new browser context
BrowserContext context = browser.newContext();
// 3. Open a new page (tab) inside the created browser context
Page page = context.newPage();
// 4. Navigate to the target website
// This URL contains a file upload element that we will interact with
page.navigate("https://testkru.com/Elements/Files");
// 5. Locate the single file upload input element
Locator multiFileUploadLocator = page.locator("#multiFileUpload");
// 6. Upload the files
multiFileUploadLocator.setInputFiles( new Path[] {
Paths.get("src/main/resources/plant_image.jpg"),
Paths.get("src/main/resources/codekru.png")
});
}
}
The above code will upload both images to the target element.


Clearing Uploaded Files
Sometimes you need to remove an already attached file before uploading a new one.
So, to remove already attached files, pass an empty array:
multiFileUploadLocator.setInputFiles(new Path[0])
Let’s see this in action with an example. We’ll use the same code from the multiple file upload scenario, but this time, we’ll add the line shown above to clear the uploaded files.
import com.microsoft.playwright.*;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CodekruTest {
public static void main(String[] args) {
// Initialize the Playwright library (required to use Playwright APIs)
Playwright playwright = Playwright.create();
// 1. Launch the Chromium browser instance
// setHeadless(false) → Launches the browser in a visible UI mode (not headless)
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
// 2. Create a new browser context
BrowserContext context = browser.newContext();
// 3. Open a new page (tab) inside the created browser context
Page page = context.newPage();
// 4. Navigate to the target website
// This URL contains a file upload element that we will interact with
page.navigate("https://testkru.com/Elements/Files");
// 5. Locate the single file upload input element
Locator multiFileUploadLocator = page.locator("#multiFileUpload");
// 6. Upload the files
multiFileUploadLocator.setInputFiles( new Path[] {
Paths.get("src/main/resources/plant_image.jpg"),
Paths.get("src/main/resources/codekru.png")
});
multiFileUploadLocator.setInputFiles(new Path[0]);
}
}
If you run the above program, you’ll see that the two images are first uploaded and then removed.
What happens if we try to pass multiple files to an element that only allows a single file upload?
Let’s try to do it with the below element –



We will try to upload two images ( plant_image.jpg and codekru.png ) using the below code
Locator fileUploader = page.locator("#singleFileUpload");
fileUploader.setInputFiles( new Path[] {
Paths.get("src/main/resources/plant_image.jpg"),
Paths.get("src/main/resources/codekru.png")
});
Whole code
import com.microsoft.playwright.*;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CodekruTest {
public static void main(String[] args) {
// Initialize the Playwright library (required to use Playwright APIs)
Playwright playwright = Playwright.create();
// 1. Launch the Chromium browser instance
// setHeadless(false) → Launches the browser in a visible UI mode (not headless)
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
// 2. Create a new browser context
BrowserContext context = browser.newContext();
// 3. Open a new page (tab) inside the created browser context
Page page = context.newPage();
// 4. Navigate to the target website
// This URL contains a file upload element that we will interact with
page.navigate("https://testkru.com/Elements/Files");
// 5. Locate the single file upload input element
Locator fileUploader = page.locator("#singleFileUpload");
// 6. Upload the files
fileUploader.setInputFiles( new Path[] {
Paths.get("src/main/resources/plant_image.jpg"),
Paths.get("src/main/resources/codekru.png")
});
}
}
Output –
Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
message='Error: Non-multiple file input can only accept single file
name='Error
stack='Error: Error: Non-multiple file input can only accept single file
We encountered an error. So, make sure not to send multiple files to elements that only allow a single file.
Downloading Files in Playwright Java
When you click a download link or button, the browser typically sends an HTTP request to fetch the file, which then prompts the user to save it locally.
In automation, you cannot rely on the OS “Save As” dialog because it is outside the browser’s DOM. Playwright solves this by listening for the download event triggered by the browser and capturing it programmatically.
Let’s see it with an example.
Let’s take the highlighted element, which downloads an image when the button is clicked. The download takes about 5 seconds to complete. We’re intentionally using such an element to demonstrate that Playwright will wait for the download to be ready before continuing with the execution.


Downloading a file is straightforward, we just click the button that triggers the download. The real question is: how do we access or save the downloaded file so we can verify its contents or perform similar checks?
So, let’s take it step by step.
Locate the element: The element’s id is “delayedDownloadBtn”, so we can use it to find the element and perform the click operation.
Locator downloadButton = page.locator("#delayedDownloadBtn");
Wait for the download to start: We can use page.waitForDownload() to wait for the download to begin. Inside its lambda function, we’ll click the download button to trigger the download event.
Download download = page.waitForDownload(() -> {
// Perform the action that initiates download
downloadButton.click();
});
That’s it! The file will be downloaded and stored in a temporary folder, which we can access using the download.path() function.
Whole code
import com.microsoft.playwright.*;
public class CodekruTest {
public static void main(String[] args) {
// Start Playwright
Playwright playwright = Playwright.create();
// Launch Chromium browser in visible mode
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
// Create a fresh browser context
BrowserContext context = browser.newContext();
// Open a new tab
Page page = context.newPage();
// Navigate to the file upload/download page
page.navigate("https://testkru.com/Elements/Files");
// Locate the delayed download button
Locator downloadButton = page.locator("#delayedDownloadBtn");
// Start timer
long startTime = System.nanoTime();
// Wait for download to complete after clicking the button
Download download = page.waitForDownload(downloadButton::click);
// End timer and calculate elapsed time in seconds
long endTime = System.nanoTime();
double timeElapsed = (endTime - startTime) / 1_000_000_000.0;
System.out.println("Time elapsed in seconds: " + timeElapsed);
// Show downloaded file's temporary path
System.out.println("Downloaded temporary file at: " + download.path());
// Close browser and Playwright
browser.close();
playwright.close();
}
}
Output –
Time elapsed in seconds: 5.36814775
Downloaded temporary file at: /var/folders/r5/k_kpd00122jg59wzn_3dstvh0000gq/T/playwright-artifacts-DcnCz3/4e181c67-92ae-4cc2-8b4f-54deeafb6a86
This confirms that the program paused for about 5 seconds at the page.waitForDownload() line.
Save downloaded file at a specified location
We can also save the file to a specific location using the code below:
download.saveAs(Paths.get("src/main/resources", download.suggestedFilename()));
- We are saving the file in “src/main/resources” directory.
- download.suggestedFilename() gets the file name suggested by the server or browser.
Whole code –
import com.microsoft.playwright.*;
import java.nio.file.Paths;
public class CodekruTest {
public static void main(String[] args) {
// Start Playwright
Playwright playwright = Playwright.create();
// Launch Chromium browser in visible mode
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
// Create a fresh browser context
BrowserContext context = browser.newContext();
// Open a new tab
Page page = context.newPage();
// Navigate to the file upload/download page
page.navigate("https://testkru.com/Elements/Files");
// Locate the delayed download button
Locator downloadButton = page.locator("#delayedDownloadBtn");
// Start timer
long startTime = System.nanoTime();
// Wait for download to complete after clicking the button
Download download = page.waitForDownload(downloadButton::click);
// End timer and calculate elapsed time in seconds
long endTime = System.nanoTime();
double timeElapsed = (endTime - startTime) / 1_000_000_000.0;
System.out.println("Time elapsed in seconds: " + timeElapsed);
// save at a specified location
download.saveAs(Paths.get("src/main/resources", download.suggestedFilename()));
// Close browser and Playwright
browser.close();
playwright.close();
}
}
This is it. We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.