AutoIT

Upload multiple files in Selenium webdriver using AutoIT

This is my one of my favorite topic Upload multiple files in Selenium using AutoIT.

Recently in one of my requirement, we had to upload multiple times. We already know how to upload files in AutoIT but it is not recommended to create multiple AutoIT scripts for uploading files.

 

We can also upload files in Selenium using Sikuli and Robot class as well but AutoIT is the best way to do this.

 

I did some googling and finally came to know that using Command Line Parameter in AutoIT we can achieve this.

In this post, I am going to share how to parameterize AutoIT script and integrate into Selenium webdriver.

Upload multiple files in Selenium

 

I have created the video for the same.

 

 

Step 1- Edit AutoIT script

ControlFocus(“File Upload”,””,”Edit1″)
ControlSetText(“File Upload”,””,”Edit1″,$CmdLine[1])
ControlClick(“File Upload”,””,”Button1″)

 

If you focus on line number 2 where I have used $CmdLine[1] this is the way to pass parameter at run time.

In the same way, we can pass multiple arguments in AutoIT.

Example are-

$CmdLine[0] ; Contains the total number of items in the array.
$CmdLine[1] ; The first parameter.
$CmdLine[2] ; The second parameter.
$CmdLine[nth] ; The nth parameter e.g. 10 if the array contains 10 items.

 

Step 2- Pass parameters from Selenium script at run time.

Runtime.getRuntime().exec(“C:\\path \\filename.exe”+” “+”file which you want to upload.”);

 

Program to upload multiple files in Selenium Webdriver using AutoIT

package AutoITDemo;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleMultipleWindow 
{

@Test
public void uploaMultipleFiles() throws Throwable
{
WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("file:///C:/Users/Mukesh_50/Desktop/My%20blog/AutoIT/Demo.html");

driver.findElement(By.xpath(".//*[@id='1']")).click();

Runtime.getRuntime().exec("C:\\Users\\Mukesh_50\\Desktop\\My blog\\AutoIT\\fileUpload3.exe"+" "+"C:\\Users\\Mukesh_50\\Downloads\\VerifyTitle.java");

Thread.sleep(2000);

driver.findElement(By.xpath(".//*[@id='1']")).click();

Thread.sleep(2000);

Runtime.getRuntime().exec("C:\\Users\\Mukesh_50\\Desktop\\My blog\\AutoIT\\fileUpload3.exe"+" "+"C:\\Users\\Mukesh_50\\Downloads\\HATHWAY.txt");
}
}

 

You can try this with Authentication as well in which you can pass username and password at run time which will save a lot of time for you.

If you want to explore authentication in Selenium using AutoIT then below post will help you.

 

How to handle authentication window in Selenium Webdriver using AutoIT.

 

I tried this multiple times and worked always. You can also try in local machine and let me know if you are still facing some issue.

 

 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

23 thoughts on “Upload multiple files in Selenium webdriver using AutoIT

  1. Gauri says:

    Hello Mukesh sir.
    Thanks for ythe Video for AutoIT. Its really helpful.

    I want to upload multiple file
    Test case – Click on upload button from Web page -> Attach one file -> return to Web Page -> click on Upload button -> attach one file.
    To achieve this I use below code :-
    WinActivate(“Open”)
    ControlFocus(“Open”, “”, “Edit1”)
    Sleep(1000)
    ControlSetText(“Open”,””,”Edit1″,$CmdLine[1])
    Sleep(700)
    ControlClick(“Open”,””,”Button1″)
    Sleep(5000)

    But when I am executing it, first file gets attached successsfully, but for second file, popup gets open but focus doesnt goes to “Edit1 control” and it doesnt click on Open button . Instead focus goes to the Web page and the next action continues on the Web page. Open dialog remains there on the screen.

    To resolve the issue, but no luck
    1. Added wait in AutoIT script
    2. Added wait in selenium script.
    Please suggest where I’m doing wrong.

    Regards
    Gauri

    1. Hi Gauri, in this case you need to call this different times with different arguments.
      Some actions
      Runtime.getRuntime().exec(“C:\\Users\\Mukesh_50\\Desktop\\My blog\\AutoIT\\fileUpload3.exe”+” “+”C:\\Users\\Mukesh_50\\Downloads\\HATHWAY1.txt”);
      Some Actions
      Runtime.getRuntime().exec(“C:\\Users\\Mukesh_50\\Desktop\\My blog\\AutoIT\\fileUpload3.exe”+” “+”C:\\Users\\Mukesh_50\\Downloads\\HATHWAY2.txt”);
      Some more actions
      Runtime.getRuntime().exec(“C:\\Users\\Mukesh_50\\Desktop\\My blog\\AutoIT\\fileUpload3.exe”+” “+”C:\\Users\\Mukesh_50\\Downloads\\HATHWAY3.txt”);

  2. pouria says:

    Hi
    Does it work with c# too?

    1. Hi Pouria,

      AutoIt works only on Windows OS. Programming language is no barrier over here

  3. Shristi says:

    Hi Sir,
    The video doesn’t have sound.
    Can you please re-upload the unmuted version.
    Thanks again.

    1. Hi Shristi,

      A new set of videos are on the way.

  4. Premkumar says:

    hi Mukesh,
    i am getting below error while uploading the file

    INFO: Detected dialect: W3C
    Exception in thread “main” org.openqa.selenium.InvalidArgumentException: invalid argument
    (Session info: chrome=93.0.4577.82)
    Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’
    System info: host: ‘CHEAC3441L3599’, ip: ‘192.168.225.80’, os.name: ‘Windows 8.1’, os.arch: ‘amd64’, os.version: ‘6.3’, java.version: ‘16.0.1’

    1. Hi Premkumar,

      Based in the URL shared, I can see for Choose File button. You can directly sendKeys action here with file path. This will do upload file action. Please go through initial part of this link https://vistasadprojects.com/mukeshotwani-blogs-v2/upload-file-in-selenium-webdriver-using-robot-class/

  5. divya says:

    Hi,i tried to upload multiple files using AutoIT
    I think it will be helpful for someone
    ControlFocus(“Open”,””,”ToolbarWindow323″)
    Sleep(300)
    ControlSetText(“Open”,””,”ToolbarWindow323″,”E:\divya\pythonProject\99_22″)
    sleep(2000)
    ControlFocus(“Open”,””,”Edit1″)
    Sleep(200)
    ControlSetText(“Open”,””,”Edit1″,”E:\divya\pythonProject\99_22″)
    Sleep(300)
    ControlClick(“Open”,””,”Button1″)
    sleep(1000)
    MouseClick(“left”,650,360,1,20)
    sleep(1000)
    Send(“^a”)
    sleep(300)
    ControlClick(“Open”,””,”Button1″)

  6. Pooja says:

    I need to download a file and upload the same file .also i need to do this for multiple customers.For 1 customer download a file and upload it .similarly need to do this for multiple customers.

    1. Hi Pooja, where you are facing issue? I mean while uploading or downloading?

  7. satveer bajwa says:

    Hi Mukesh,
    I am trying to upload multiple files but I am getting error this file not found . Please check code

    //click on upload option

    driver.findElement(By.xpath(“/html/body/div[2]/div[5]/div/div/div/div[2]/div[2]/div[1]/div/div[1]”)).click();

    //upload pictures using Auto It
    Runtime.get Runtime().exec(“E:\\uploadpic multifinAL.exe”+” “+”E:\\QA\\upload test pictures\\14-dc-wedding-photos-white-umbrella.jpg”);
    //click on upload option

    driver.findElement(By.xpath(“/html/body/div[2]/div[5]/div/div/div/div[2]/div[2]/div[1]/div/div[1]”)).click();

    //upload pictures using Auto It
    Runtime.getRuntime().exec(“E:\\uploadpic multifinAL.exe”+” “+”E:\\QA\\upload test pictures\\beautiful_fantasy_world-wide.jpg”);

    1. Hi Satveer,

      File not found is not an info message that path/format of path which you have mentioned is incorrect. Kindly check it again

      1. satveer bajwa says:

        Hi Mukesh,
        Thank you for your answer, I tried with other method like I added mutiples files in the editor so It works now.
        ControlSetText(“File Upload”,””,”Edit1″,”””E:\QA\upload test pictures\abc.jpg””””E:\QA\upload test pictures\mother-and-baby-indoors-hugging-and-smiling.jpg”””

        Thank you again.

        1. Great Satveer…:)

  8. HarishPrabhu says:

    Hi Mukesh,
    How to upload different files in multiple windows in selenium java?

    1. Hi Harish, In this case, you need to pass files as a parameter.

  9. Abhilash Reddy.S says:

    hi Mukesh,

    while i am running mt code for multiple file uploads, I am getting this below error. Please help me to get ride it.

    Exception in thread “main” java.io.IOException: Cannot run program “G:\Selenium”: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)

    1. Hi Abhilash,

      Did you provide actual path of AutoIt executable file because from error, it is saying that specified file path is not available. Kindly check your code once again.

  10. jayashree says:

    Hey hi mukesh,
    when m trying to upload the multiple files using Auto it i got this error”array variable has incorrect number of subscripts or subscript dimension range exceeded autoit”

    1. Hi Jayashree,

      Are you passing exact file path and syntactically correct?

  11. Arun Jadhav says:

    Hi Mukesh,

    How do we upload multiple files using single Runtime statement and multiple Commandline parameters in the autoit script?

    Like, I would like to provide multiple input parameters as the files using the following
    ControlSetText(“File Upload”,””,”Edit1″,$CmdLine[1])

    the Runtime.getRuntime.exec(“Path to .exe file”+” “+”list of file names to be provided here”); statement must pick multiple files from the input parameters

    1. Hi Arun,

      As per my video you can pass multiple files but one by one as parameter.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.