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.
3 Answers
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)]);
}
}
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
If you are looking for the multiple file upload just in one click of upload button, then hope this will work for you:-
Here
-
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?– DhairyaCommented 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
sendKeys("file1.txt;file2.jpg")