How To Upload Multiple Files In Selenium WebDriver – Java

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.

Let’s see sample code.

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

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

3 thoughts on “How To Upload Multiple Files In Selenium WebDriver – Java”

  1. Hi Amod, why are you using getAbsolutePath() for the first file only, not for the subsequent file?

    1. Hey its not intentional. getFileFromResources() returns File type. So I am getting absolute path as String so that I can concatenate both file paths using \n. You can skip use of getAbsolutePath() as when we concatenate with \n it is converted in String. Both below statements will work :-

      String filesPathSeperaedWithNewLineChar = getFileFromResources(“images/amazon-fire-tv-stick-4k-4-3h9r-3h9r.jpg”).getAbsolutePath()+”\n”+getFileFromResources(“images/maxresdefault.jpg”).getAbsolutePath();

      String filesPathSeperaedWithNewLineChar = getFileFromResources(“images/amazon-fire-tv-stick-4k-4-3h9r-3h9r.jpg”)+”\n”+getFileFromResources(“images/maxresdefault.jpg”);

      But this will not work :-
      String filesPathSeperaedWithNewLineChar = getFileFromResources(“images/amazon-fire-tv-stick-4k-4-3h9r-3h9r.jpg”) + getFileFromResources(“images/maxresdefault.jpg”);

Leave a Reply

Your email address will not be published. Required fields are marked *