Skip to content

Upload multiple files in Selenium

Upload multiple files in Selenium

Hi friends, in this post I will show you how to upload multiple files in Selenium. Selenium can not interact with the popup window for files selection. But Selenium has the sendKey() method that support for file upload.

Let’s checkout below line of codes:

That’s all you need. The file will be set to the input element and you can click submit button to upload the file.

But how to upload multiple files? => It’s easy with sendKey method! Just put a new line character “\n ” between your files.

Importance 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!

By using Selenium’s sendKey() method, you can not see the upload window popup. But if you want to handle that popup then the 3 solution below will help you.

Java Robot class with Selenium

This part I will show you how to upload multiple files using key board to handle the popup dialog. This solution will work like below description:

  • Click on the upload button to show up the upload window dialog
  • Using Robot class to press on keyboard, press CONTROL + L to select the address bar => set the folder where you need to upload your files to clipboard then paste into address bar => press ENTER key.
  • Press CONTROL + ENTER to focus on file name box
  • Combine all the files you need to upload into a string like this (Each file name is inside a double quote pair)
  • Put this string into clipboard, and press CONTROL + V to paste it into file name box
  • Press ENTER to select files and close the upload window.

AutoIT with Selenium

Another solution to upload multiple files via window popup is using AutoIT tool. AutoIT is a scripting language to handle window GUI object.

Below is a sample AutoIT script to handle file upload

Save above code into FileUpload.au file => From Java code, let’s call AutoIT3.exe to combine above script and run it

Sikulix with Selenium

This is the last solution that I have been tried to upload multiple files. Sikulix is a framework that support for automating any things on screen. Any things you can see on screen then Sikulix can see and interact with it. Sikulix is using OpenCV as it core to recognize the element on screen based on it’s image. Your job is quite easy, just gave to Sikulix an image and ask it to do some thing like mouse click, enter text. Sikulix then will find on your screen the position of that image, then do the job. There is a very detailed tutorial about working with Sikulix for reference.

Sumary

I have introduced to you 4 solutions to upload multiple files when automate your test using Selenium. Based on your demand, you can choose any solution to adapt to your project. Feel free to put comments below to share with me any problems!

Published inAutomation TestJavaProgramming

2 Comments

    • tuyennta tuyennta

      Hi Alex,
      Thank you for your reply on my post. I have read your suggested post, it’s a very good post about Sikuli for reference. I have updated my post to link to yours.
      Thanks,
      Tuyen

Leave a Reply

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