Make Selenium Easy

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.

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.

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.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

How To Upload Multiple Files In Selenium WebDriver – Java

by Amod Mahajan time to read: 2 min
%d bloggers like this: