5

In automation testing, I have successfully uploaded a single file multiple times using Selenium webDriver, but the project I'm working on now requires multiple file upload at a single time. So, my automation test case also has multiple uploads at once. But I have no clue how to add multiple files at once in Selenium.

6
  • How would it be done manually? Most of the time, if multiple file selections are allowed via a dialog, when you click OK on the box, it populates the files separated by semicolons. If that's the case, just send your list of files separated by semicolons. Commented Mar 27, 2019 at 14:37
  • I am completely baffed as to why this question has three upvotes and the posters own "answer" which does not seem to me to even pertain to his own question gets two more upvotes. Neither the question nor his answer makes any sense. Commented Mar 28, 2019 at 13:30
  • Hey Bill, tried your suggestion, using semicolons. It didn't work for me. I added two file paths like sendKeys("file1.txt";"file2.jpg"). It shows compile time error, whereas if we separate it by comma like sendKeys("file1.txt","file2.jpg"), this thing didn't show compile time error but shows run time error. Correct me if I've done anything wrong. One more thing, for this query of mine. I googled & even searched for the solution here as well. But couldn't find, that's why I added this question.
    – Dhairya
    Commented Mar 28, 2019 at 13:53
  • Pass it as one string - concatenate the semicolon INSIDE the quotations, i.e. sendKeys("file1.txt;file2.jpg") Commented Mar 28, 2019 at 13:56
  • Thanks, @BillHileman, but this thing either didn't work.
    – Dhairya
    Commented Mar 29, 2019 at 9:31

3 Answers 3

4

In my case, I am already using a CSV file to fill data. So I made logic to add multiple files using the CSV file which I was using to add data (Form Filling).

Each Form filling data is stored in a row, so to fill new form I jumped to the next row and filled the form. So to add multiple files in a single form I added file paths like form data in a single row. So each cell in a row contains a separate file path. And then I fetched the file paths in a row using CSVReader & readNext for each and every cell I find the last cell in a row. That's how I was able to add multiple files.

Sample Code:-

for(int i=1 ;i<strs.size() ;i++) //loop for scanning row by row data
    {
        for(int j=7;j<=strs_2.length;j++) //loop for scanning multiple file paths in a single row
        {
             driver.findElement(By.xpath("xpath of element")).sendKeys(csvCell[(j)]);
        }
    }
1

It’s easy with sendKeys method to upload multiple file in one go. Just put a new line character “\n ” between your files. See below sample code.

WebElement inputElement = driver.findElement(By.xpath("xpath of input element"));
String uploadFilePath = "C:/myfile.txt";
String uploadFilePath2 = "C:/myfile2.txt";
String uploadFilePath3 = "C:/myfile3.txt";
inputElement.sendKeys(uploadFilePath + "\n " + uploadFilePath2 + "\n " + uploadFilePath3);

Important note: new line character is placed between your files, if you combine a string ending with a new line character then you will get the file not found error!

Hope it will help you

0

If you are looking for the multiple file upload just in one click of upload button, then hope this will work for you:-
Here

2
  • Thanks, mate for the solution. But the link you provided has a solution based on AutoIt (It is only supported in Windows). And I'm using an Ubuntu system. So, can you provide a solution for Ubuntu?
    – Dhairya
    Commented Mar 27, 2019 at 13:21
  • I have no idea about Ubuntu, i think now you have one way to do this by using Robot class it will make you script's LOC long, and for further info about Ubuntu you can take help from Here Commented Mar 27, 2019 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.