Upload File in Playwright Java

Profile picture for user arilio666

To upload a file using Playwright Java, you can follow these steps:

  • Create a new instance of the Playwright class and launch a browser.
  • Navigate to the page where you want to upload the file.
  • Locate the file input element on the page using the selector.

In this case, we will be using the autopract upload site.

It should have the input tag and type within to upload a file for this to work.


        Playwright playwright = Playwright.create();
        Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
        Page page = browser.newPage();
        page.navigate("http://autopract.com/selenium/upload1/");
        page.setInputFiles("//input[@type='file']",
                Paths.get("C:\\Users\\arili\\git\\Assignments\\Selenium\\target\\CRED.xlsx"));
                
  

              
Call the setInputFiles method on the element to set the file path to upload.

We can see here the file got uploaded, and this is how we can upload a file in playwright using Java.