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.
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.
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
Mukesh Otwani says
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”);
pouria says
Hi
Does it work with c# too?
Mukesh Otwani says
Hi Pouria,
AutoIt works only on Windows OS. Programming language is no barrier over here
Shristi says
Hi Sir,
The video doesn’t have sound.
Can you please re-upload the unmuted version.
Thanks again.
Mukesh Otwani says
Hi Shristi,
A new set of videos are on the way.
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’
Mukesh Otwani says
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://learn-automation.com/upload-file-in-selenium-webdriver-using-robot-class/
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″)
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.
Mukesh Otwani says
Hi Pooja, where you are facing issue? I mean while uploading or downloading?
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”);
Mukesh Otwani says
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
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.
Mukesh Otwani says
Great Satveer…:)
HarishPrabhu says
Hi Mukesh,
How to upload different files in multiple windows in selenium java?
Mukesh Otwani says
Hi Harish, In this case, you need to pass files as a parameter.
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)
Mukesh Otwani says
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.
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”
Mukesh Otwani says
Hi Jayashree,
Are you passing exact file path and syntactically correct?
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
Mukesh Otwani says
Hi Arun,
As per my video you can pass multiple files but one by one as parameter.