TransWikia.com

How to add multiple files in Selenium Web-driver using Java

Software Quality Assurance & Testing Asked by Dhairya on October 25, 2021

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)]);
        }
    }

Answered by Dhairya on October 25, 2021

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

Answered by Muzzamil on October 25, 2021

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

Answered by Upkar Singh on October 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP