Yahya Hussein
Yahya Hussein

Reputation: 9121

How to select many files using Windows file explorer with selenium webdriver

I am automating a UI test where selecting file to upload is involved, I was able to automate the file selection using this solution.

WebElement filepath=driver.findElement(By.id("fileUploadId"));
filepath.sendKeys("C:\\TextFile.txt");

My issue is that I need to select many files to upload, is there a special format I should follow in the path I am sending? because I tried space-separated paths and it didn't work.

Upvotes: 3

Views: 2636

Answers (2)

timmacp
timmacp

Reputation: 193

sendKeys() with a newline character (\n) might not work consistently across different browsers and operating systems, it doesn't work for me with chrome on windows 11.

I just use multiple calls to sendKeys, as mentioned here:

http://makeseleniumeasy.com/2020/06/14/how-to-upload-multiple-files-in-selenium-webdriver-java/

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

To upload multiple files you can construct the character string adding all the absolute path of the files seperated by \n as follows:

WebElement filepath = driver.findElement(By.id("fileUploadId"));
filepath.sendKeys("C:/TextFile1.txt \n C:/TextFile2.txt \n C:/TextFile3.txt");

References

You can find a couple of relevant detailed documentations in:

Upvotes: 3

Related Questions