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.
#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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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

    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
2
%d bloggers like this: