Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Upload Multiple Files In Selenium WebDriver – Java

Posted on 02/19/2025 By admin

We come across many scenarios where we need to upload a file or files if it supports multiple upload. We already learnt to upload a file using Selenium WebDriver for Java binding.

If A file upload web element is created using “input” tag with type as “file”. An input tag is used to accept some value or user action. This input tag makes it very simple to upload a file through selenium webdriver. You just need to use sendKeys() method of selenium webdriver and send path of file as argument. You can refer complete article on Uploading a file in Selenium WebDriver here.

Let’s complicate it little bit. Uploading a file is easy but what if we need to upload multiple files?

There are two solutions of it :-

  1. Use multiple sendKeys() commands for each file
  2. Concatenate path of all files which you want to upload using “\n“.

I have kept sample images in to a sub folder “images” under resource folder. To get path of those files I am using an abstract class ClassLoader.

package SpecialConcepts;

import java.io.File;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class UploadMultipleFiles {
        
        // This method helps you to fetch path of any file from resource folder
        private File getFileFromResources(String fileName) {
        ClassLoader classLoader = getClass().getClassLoader();
        URL resource = classLoader.getResource(fileName);
        if (resource == null) {
            throw new IllegalArgumentException("file is not found!");
        } else {
            return new File(resource.getFile());
        }
    }

        @Test
        public void MultipleFileuploadInChromeUsingNewLineChar() {

                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_multiple");
                String filesPathSeperaedWithNewLineChar = getFileFromResources("images/amazon-fire-tv-stick-4k-4-3h9r-3h9r.jpg").getAbsolutePath()+"\n"+getFileFromResources("images/maxresdefault.jpg");
                driver.switchTo().frame("iframeResult").findElement(By.id("files")).sendKeys(filesPathSeperaedWithNewLineChar);
                driver.close();
        }
        
        @Test
        public void MultipleFileuploadInFirefoxNewLineChar() {

                WebDriverManager.firefoxdriver().setup();
                WebDriver driver = new FirefoxDriver();
                driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_multiple");
                String filesPathSeperaedWithNewLineChar = getFileFromResources("images/amazon-fire-tv-stick-4k-4-3h9r-3h9r.jpg").getAbsolutePath()+"\n"+getFileFromResources("images/maxresdefault.jpg");
                driver.switchTo().frame("iframeResult").findElement(By.id("files")).sendKeys(filesPathSeperaedWithNewLineChar);
                driver.close();
        }
        
        @Test
        public void MultipleFileuploadInChromeUsingMultipleSendKeysMethod() {

                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_multiple");
                String file1 = getFileFromResources("images/amazon-fire-tv-stick-4k-4-3h9r-3h9r.jpg").getAbsolutePath();
                String file2 = getFileFromResources("images/maxresdefault.jpg").getAbsolutePath();
                driver.switchTo().frame("iframeResult").findElement(By.id("files")).sendKeys(file1);
                driver.findElement(By.id("files")).sendKeys(file2);
                driver.close();
        }
        
        @Test
        public void MultipleFileuploadInFirefoxUsingMultipleSendKeysMethod() throws InterruptedException {

                WebDriverManager.firefoxdriver().setup();
                WebDriver driver = new FirefoxDriver();
                driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_multiple");
                String file1 = getFileFromResources("images/amazon-fire-tv-stick-4k-4-3h9r-3h9r.jpg").getAbsolutePath();
                String file2 = getFileFromResources("images/maxresdefault.jpg").getAbsolutePath();
                driver.switchTo().frame("iframeResult").findElement(By.id("files")).sendKeys(file1);
                driver.findElement(By.id("files")).sendKeys(file2);
                driver.close();
        }

}

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Are We Using Proper Wait In Proper Way In Selenium Webdriver?
Next Post: rolling file appender in log4j2

Related Posts

Part 5: Waits In Selenium: What Happens When We Mix Sleep With Other Types Of Waits ? Uncategorized
image – Make Selenium Easy Uncategorized
July 2019 – Make Selenium Easy Uncategorized
May 24, 2017 – Make Selenium Easy Uncategorized
August 22, 2018 – Make Selenium Easy Uncategorized
Untitled Diagram (6) – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark