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:

 

 
public class MultiFileUpload {

public static void main(String[] args) throws IOException, InterruptedException, Exception {

System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");

WebDriver driver= new ChromeDriver();

driver.get("file:///C:/Users/amodm/Desktop/demo.html");

driver.findElement(By.name("upload")).click();

Thread.sleep(5000);

Runtime.getRuntime().exec("C:\\Users\\amodm\\Desktop\\script.exe");

}

}
 

 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.

 

 

 

7 thoughts on “Upload multiple files in Selenium

  1. Hi Amod,
    Thanks for your valuable information I am trying to upload multiple files using autoit but I am getting “error in expression” while open the upload pictures dialog. let me know how can I solve this issue .

  2. Hi Amod Mahajan,

    I have tried your code . I can not able to run script successfully . the driver clicked the upload button after that there is error path is not valid. I have tried the same way you mentioned above .

  3. Hi Amod! usefull one
    But $vVariable = “””F:2017MakeSeleniumEasySuportedFilesdemo2.txt”” “”F:2017MakeSeleniumEasySuportedFilesdemo1.txt””” is bit confusing quoting

Leave a Reply

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