Upload multiple files in Selenium

Hello Folks,

This is an advanced concept. Beginners can ignore it as of now. I just found it while automating some functionality in mu company. So just thought of sharing.

Note: I assume, you have basic knowledge of AutoIT.

During automating the application, we come up with scenarios where we need to upload file. If it is single file upload, we can do it using sendKeys() method or AutoIT script. Problem happens when we need to upload a multiple files.

To upload multiple files we use ctrl key and select whatever files we want to upload and click on “Open” button.

But how to do it in Selenium??

After so many attempts of try, I finally got one solution, which I will share here.

HTML code of file upload input tag:

<input type=”file” name=”img” multiple>

When we write “multiple” within tags, it becomes multiple file upload enable input tag. Save above html code with .html/.htm extension and open it and click on ‘choose file’.

How did I get an Idea?

When we select single file to upload, notice “File Name” text box:

singlefile

When we select multiple files using ctrl key, then notice “File Name” text box:

multifile

Multiple file names are put in double quotes separately.

I replaced file names with absolute path as below:

absolutepath

And when I clicked on Open, it uploaded:

uploadsuccess

So, I concluded, if I am able to pass below string to the File Name text box, I can upload multiple files in single shot.

“F:\2017\MakeSeleniumEasy\SuportedFiles\demo2.txt” “F:\2017\MakeSeleniumEasy\SuportedFiles\demo1.txt”  

Now I stared exploring my options:

1.Using sendKeys():

When I passed above string, it gave error like its not absolute xpath. I used escape sequence to send double quotes but no luck.

2. Using Robot class:

It includes lot of key press and release which is irritating. I didn’t feel it easy.

3. Using AutoIT:

This worked!!!

In AutoIT , we can parameterized ControlSetText method. And I passed required string to ControlSetText method as below:

$vVariable = “””F:\2017\MakeSeleniumEasy\SuportedFiles\demo2.txt”” “”F:\2017\MakeSeleniumEasy\SuportedFiles\demo1.txt”””

ControlFocus(“Open”, “”, “Edit1”)

ControlSetText(“Open”, “”, “Edit1” ,$vVariable)

ControlClick(“Open”, “”, “Button1”)

Note: To pass double quotes in AutoIT script, we need to pass in to double quote only.

 I saved and compiled it.

JAVA code:

 

 Please try above code and let me know if it works file. As I assume, you will be aware of that AutoIT works only in windows and code is different for each browser. So, kindly update the code as per your requirement.

Note: Kindly excuse for formatting as I am new to wordpress and still I need to learn many things.

 

 

 

Author: Amod Mahajan