• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / AutoIT / Upload File in Selenium Webdriver using Autoit

Upload File in Selenium Webdriver using Autoit

March 16, 2020 by Mukesh Otwani 63 Comments

Upload File in Selenium Webdriver using Autoit

This guide will show you how to Upload File in Selenium Webdriver using Autoit Step by Step guide in a simple manner.

1-While automating web-application many times you will get window-based activity like- file upload, file download pop-up, window authentication for secure sites, etc. In this case, Selenium will fail and will not be able to handle desktop elements.

We can upload files in Selenium using different ways

1- Using sendKeys method

2- Using Robot class

3- Using AutoIT

4- Using WGET

Let me explain to you the easiest way which works most of the time using sendKeys

Syntax

driver.findElement(By.xpath("xpath of any element")).sendKeys("File Path");

Example

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\\Downloads\\Mukesh.png");

or

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:/Downloads/Mukesh.png");

Explanation- You do not have to click on file uploader because if you click on the file upload button, it will start file uploader which you need to handle separately.

You can find the locator of the element where the file needs to be updated and then you can pass the file path directly, files can be excel, png or any type depends on your application.

Note- In case of sendKeys method does not work then you can go ahead with all other options.

 

Introduction to AutoIT tool

1-AutoIt is a freeware automation tool that can work with desktop applications too.

2-It uses a combination of keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys).

For more info about AutoIT, you can visit their Official Website AutoIt Official Website

Upload File in Selenium Webdriver using Autoit

 

YouTube video for Same 

                    
How to write a script in AutoIT?

For AutoIt scripting, you should have three things ready.

1-AutoIt Editor- Editor helps us to write AutoIt scripts.

2-Tool Finder – It will help us to identify the element and check their Attributes.

3- AutoIt Help section- This help you to understand about AutoIt functions and what are the parameter it accepts.

Let’s start with Downloading first

Step 1– Navigate to AutoIt  official website  https://www.autoitscript.com/site/autoit/downloads/  and go to download section or Click here Download AutoIt

Step 2– Click on Download AutoIt and Install

Step 3–  Click on Download Editor and Install.
Upload File in Selenium Webdriver using Autoit

 

Step 4– Once both are installed in your machine, check all are installed correctly.

Note- Generally it goes to C:\Program Files\AutoIt3 location if you do not change it

Upload File in Selenium Webdriver using Autoit

 

 

 Step5– Open SCiTE folder and  Click on SciTE this will open AutoIt Editor

 

Upload File in Selenium Webdriver using Autoit
Once all Installed Let’s see how we can write the script.

 

Upload File in Selenium Webdriver using Autoit

To Upload File in Selenium Webdriver using AutoIt we need to take care of some steps so let’s begin

To upload a file in Selenium Webdriver we will create AutoIT script, which will handle file-uploaded window, and then we will combine Selenium script with AutoIt scripts.

Click on the Upload button you will get a file uploader we will handle the same using AutoIt.

 

Step 1- Open Editor and Finder Tool

Upload File in Selenium Webdriver using Autoit

 

Upload File in Selenium Webdriver using Autoit

 

 

 Step 2– We need to write script to upload file so we will use some method of AutoIt.
Each method will have some own functionality
ControlFocus-This will give focus on the window

ControlSetText-This will set the file path

ControlClick-This will click on button

 

Step 1- 

Click on Browse button , a new window will open now open finder tool and Click on Finder tool and drag to the file name as I shown in below screenshot.

 

Upload File in Selenium Webdriver using Autoit

This will give all the detail about that window and file name section info; we will use only some attributes like window title, class, and instance.

Open AutoIt Editor and Write Script

Upload File in Selenium Webdriver using Autoit

In ControlClick method, we will give control id of open button

Step 2-

Save the script to a particular location with some unique name.

Note- By default script will be saved as .au3 extension

Step 3– Now Compile the script so for compiling right click on file and Select compile script this will generate a .exe file of the file.

 

Upload File in Selenium Webdriver using Autoit
Step 4- Now write Selenium program and add this .exe file and run your program

Here is the code

package demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DemoFileUpload {

 public static void main(String[] args) throws Exception {
                
// This will open Firefox browser
WebDriver driver=new FirefoxDriver();
        
// This will maximize browser to full screen
driver.manage().window().maximize();
        
// This will open respective URL
driver.get("your application url");
        
// This will click on Upload button
driver.findElement(By.xpath("//*[@type='file']")).click();
     
// This will invoke AutoIT script here give the path of the script 
//and this will throw IO exception so u can use throw or try catch
// In my case I am using throws

Runtime.getRuntime().exec("C:\\Users\\mukesh_otwani\\Desktop\\AutoItScripts\\blogUpload.exe");

// Once you will run this program AutoIt script will be invoked and respective f//ile will be attached
  
  }

}

 

Thanks for visiting my blog. Please leave a comment below if you are finding any issue while Upload File in Selenium Webdriver using Autoit.

Keep in touch. Have a nice day.

Now if you are comfortable with Basic Selenium, you can check out Advance Selenium Tutorial as well. 🙂

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

Filed Under: AutoIT Tagged With: File Upload

Reader Interactions

Comments

  1. Nilesh says

    December 23, 2019 at 5:14 PM

    Hi Mukesh,

    Why It is working for single run only?

    Reply
    • Mukesh Otwani says

      December 23, 2019 at 7:33 PM

      Hi Nilesh,

      There is another post that will guide you to upload multiple files using the same AutoIT executable. Here is the link http://learn-automation.com/upload-multiple-files-in-selenium-webdriver/

      Reply
  2. Ankur says

    August 18, 2019 at 4:57 PM

    Hi Mukesh,

    Thanks for Selenium Tutorial.

    Need to know, Through AutoIT while uploading the file can we change the location of the Folder.
    Scenario:
    When click on the upload file then by default desktop item is displaying but my uploading file is placed on another Location.
    So how can we switch the folder from Desktop to other Location??

    Reply
    • Mukesh Otwani says

      August 20, 2019 at 9:56 AM

      Hi Ankur,

      Please refer http://learn-automation.com/upload-multiple-files-in-selenium-webdriver/ where you have to provide full path of file instead of file name.

      Reply
  3. Sai says

    July 17, 2019 at 8:27 PM

    Hi Mukesh,

    My Script
    ControlFocus(“Open”,””,”Edit1″)
    ControlSetText(“Open”,””,”Edit1″,”C:\Users\naresh\source\repo\QA.png”)
    ControlClick(“Open”,””,”Button1″)

    My test class runs but execution stops for few seconds at upload button and throws below exception in console and executes all other test cases after this test case.

    Exception occured with locator: By.xpath: //*[@type=’file’]

    But xpath is correct

    Please help.

    Reply
    • Mukesh Otwani says

      July 18, 2019 at 11:24 AM

      Hi Sai,

      Since I can see xpath which you have used, could please try driver.findElement(by.xpath()).sendKeys() instead of using AutoIT and let me know with your observations

      Reply
      • Sai says

        July 18, 2019 at 9:43 PM

        Hi Mukesh,

        Below code works for me,
        1. driver.findElement(By.xpath(“//*[@type=’file’]”)).sendKeys(“C:\Users\Desktop\QA.png”);
        2. elementActions.doSendKeysUsingBySelector(LogoUpload, “C:\Users\Desktop\QA.png”);

        Thank you.
        However, I would like to get rid of hardpath. My manager should be able to run on his machine. In that case I think I cannot use above code. Hence, I was trying to upload using AutoIT. Could you please help how to overcome such type of issue and what is the best way to avoid hardpath ? Thanks.

        Reply
        • Mukesh Otwani says

          July 19, 2019 at 11:57 AM

          Hi Sai,

          Better way is to keep these required files into shared folder which should be accessible to everybody or keep these files into some folder inside project folder so that whoever clones project can get these files.

          Reply
          • Sai says

            July 19, 2019 at 11:34 PM

            Hi Mukesh,

            I figured out. Below code works magically,

            public void getLogo(){
            String dirPath = System.getProperty(“user.dir”);
            String path = dirPath+”\\QA.png”;
            driver.findElement(By.xpath(“//*[@type=’file’]”)).sendKeys(path);
            }

            Thanks for your help.:)

          • Mukesh Otwani says

            July 20, 2019 at 9:54 AM

            Hi Sai,

            Yes, you are partially correct. But how will you deal with file QA.png, will this file available on other machines too. That why, I mentioned to add these files into your project folder.

  4. ranjan says

    March 26, 2019 at 2:56 PM

    Hi Mukesh,
    I am observing that sometime the exe file run successfully from code and sometime it doesnt, say out of 10 times 4 times it does not run and my test fails.
    Is there any way where i can handle this.

    Reply
    • Mukesh Otwani says

      March 26, 2019 at 7:10 PM

      Hi Ranjan,

      We can’t have control on execution of AutoIt as it doesn’t interact with Selenium anywhere. We have to adjust static time according to application behavior.

      Reply
  5. sai kiran says

    March 21, 2019 at 10:55 PM

    I am getting error such as “unkonwn file name”

    Reply
    • Mukesh Otwani says

      March 25, 2019 at 1:41 AM

      Hi Sai,

      Kindly elaborate scenario.

      Reply
  6. Siva says

    March 13, 2018 at 5:55 PM

    Hi Mukesh,
    Your tutorial is very helpful for me. Thank You!
    I am facing an issue below
    When an execute my script, it will always navigate file upload page to “Desktop” showing error as “File not found” but
    IN AUTOIT script:
    I have given attachfile path to “D:\HDimage\profile.jpeg”

    Script working fine, if attach file placed in desktop

    Reply
    • Mukesh Otwani says

      March 13, 2018 at 9:56 PM

      Hi Siva,

      File not found error is thrown by OS. It always happens when the provided path is either wrong or path mentioned in input box is syntactically wrong. Kindly recheck path you have provided.

      Reply
      • Siva says

        March 15, 2018 at 11:22 AM

        Hi Mukesh,
        Thank you for a quick response
        My script:
        ControlFocus(“Open”,””,”Edit1″)
        ControlSetText(“Open”,””,”Edit1″,”D:\HDimage\profile.jpeg”)
        ControlClick(“Open”,””,”Button1″)
        My question:
        My path: D:\HDimage\profile.jpeg but when clicking upload button, it’s open “Desktop” page.

        anything I am doing wrong? please correct me

        Reply
        • Mukesh Otwani says

          March 16, 2018 at 9:28 PM

          Hi Siva,

          Whatever location gets open by default doesn’t matter. If the absolute path of file is correct and valid then it will go ahead smoothly.

          Reply
          • sivabalan says

            March 19, 2018 at 11:15 AM

            Thank you very much!

  7. Neeru says

    March 12, 2018 at 11:01 AM

    Is there something that can be used to drag and drop a file from harddrive/desktop to UI?Just like we can do drag and drop in Gmail instead of clicking on attachment icon?

    Reply
    • Mukesh Otwani says

      March 12, 2018 at 12:55 PM

      Hi Neeru,

      You can use Sikuli or AutoIT for same. Selenium will help you to do same action inside WebUI only.

      Reply
  8. Abhishek Sharma says

    March 5, 2018 at 4:03 PM

    How can we parameterize the exe file. I want to set a text in edit box using autoIT and this text is to given by use…Can u help ?

    Reply
    • Mukesh Otwani says

      March 5, 2018 at 7:45 PM

      Hi Abhishek,

      This post may help you http://learn-automation.com/upload-multiple-files-in-selenium-webdriver/

      Reply
  9. vasantha says

    February 26, 2018 at 3:00 PM

    please tell me how handle datepicker

    Reply
    • Mukesh Otwani says

      February 26, 2018 at 4:52 PM

      Hi Vasantha,

      Please go through this link http://learn-automation.com/handle-calender-in-selenium-webdriver/

      Reply
  10. Harshada says

    February 22, 2018 at 5:52 PM

    Hello mukesh sir.
    Thank you so much for learn-automation.Its really helpful for me.

    Reply
    • Mukesh Otwani says

      February 23, 2018 at 9:23 AM

      Hi harshada,

      Thanks for your lovely comments…:)

      Reply
  11. keerthi says

    February 1, 2018 at 9:05 PM

    my machine is 32-bit os ,will autoit support for 32-bit system?

    Reply
    • Mukesh Otwani says

      February 1, 2018 at 9:35 PM

      Hi Keerthi,

      Yes, AutoIt supports both architectures.

      Reply
  12. Kaur says

    January 11, 2018 at 12:51 PM

    Hi Mukesh,
    Your selenium videos are very clear and helpful.
    Thank You

    Reply
    • Mukesh Otwani says

      January 11, 2018 at 2:18 PM

      Hi Kaur,

      Thanks for your overwhelming comment…:)

      Reply
  13. vidhya says

    November 29, 2017 at 10:17 PM

    I have query
    can we convert selenium test suite into executable file
    so that we can run when ever need

    Reply
    • Mukesh Otwani says

      December 13, 2017 at 11:28 AM

      Hi Vidhya,

      Yes, it is possible. But you need to wrap drivers and other files also. But managing it will become bit tough.

      Reply
  14. Manjunath says

    February 15, 2017 at 12:30 AM

    Runtime.getRuntime().exec(“C:\\Users\\mukesh_otwani\\Desktop\\AutoItScripts\\blogUpload.exe”);
    this is not working for me. please suggest

    Reply
    • Mukesh Otwani says

      February 15, 2017 at 2:26 PM

      Hi Manjunath,
      Did you hardcode file path inside AutoIT script ?

      Reply
  15. Aishwarya says

    February 14, 2017 at 12:14 PM

    How to attach file in selenium? i tried with the help of robot but it is not working .can u plz tell me different option

    Reply
    • Mukesh Otwani says

      February 15, 2017 at 12:01 AM

      Hi Aishwarya,

      If I am correct then attach means upload. You can upload file using autoIT. During file upload, when you click on browse button on webpage, it opens a window to select a file. And same window is coming up from OS not from webpage. That’s the reason, for file upload we need to take help of third party tool.

      Reply
  16. Deepak says

    December 26, 2016 at 4:56 PM

    i am unable to click on Browse button in IE11. script is runnig fine in chrome but in IE, it is not clicking on browse button.

    Reply
    • Mukesh Otwani says

      December 26, 2016 at 7:44 PM

      Try with CSS

      Reply
      • Deepak says

        December 27, 2016 at 4:18 PM

        Thank you so much for replying, i was able to click on browse button through js.
        Thank you so much once again 🙂

        Reply
        • Mukesh Otwani says

          December 28, 2016 at 11:21 AM

          Cheers Deepak

          Reply
  17. Ritu Singh says

    July 15, 2016 at 7:04 PM

    Hi Mukesh,

    Your blog and all the selenium videos are really very helpful,they are very clear to understand and easy to learn.

    Thank you 🙂

    Reply
    • Mukesh Otwani says

      July 20, 2016 at 2:11 PM

      Hey Ritu,

      Thanks a ton 🙂 let me know if any help from my side.

      Reply
  18. Vignesh says

    May 4, 2016 at 3:14 PM

    Hi,

    In your example you worked with Java application. In my case I am working with C#. C# is not accepting this line -> Runtime.getRuntime().exec(” “). Can you give me an alternate solution for this…

    Reply
  19. Vignesh says

    May 4, 2016 at 12:39 PM

    Hi,

    I have gone through your file uploading concept in selenium. In your example you are uploading file of type .html. Can I able to upload excel sheet to the application? If so could you please explain about that..

    Reply
  20. suresh says

    March 7, 2016 at 4:37 PM

    how to upload multiple files using Auto-it???

    Reply
    • Mukesh Otwani says

      March 11, 2016 at 6:40 PM

      Here you go https://www.youtube.com/watch?v=bQBDARRp9LE

      Reply
  21. Vikas says

    February 18, 2016 at 12:07 PM

    Hi Mukesh Otwani

    Thanks for sharing the code.

    I tried to edit the code for saving pdf file with “Save As” dialog box in Chrome browser with below code

    SaveAs.au3 file
    WinWaitActive(“Save As”,””,10)
    ControlFocus(“Save As”,””,”Edit1”)
    ControlSetText(“Save As”,””,”Edit1″,$CmdLine[1])
    ControlClick(“Save As”,””,”Button1″)

    Code inside Selenium
    Runtime.getRuntime().exec(“C:\\SaveAs.exe”+” “+”D:\\Medical.PDF”);

    My above code is not working as system open Save As window but does not pass file name which I provided and thus also does not click on save button.
    Can you please tell me where I am making mistake.

    Reply
    • Mukesh Otwani says

      February 22, 2016 at 9:08 AM

      Hi Vikas,

      Check below article to download files

      http://learn-automation.com/how-to-download-files-using-selenium-webdriver/

      Reply
  22. jyothi says

    December 18, 2015 at 9:51 PM

    This site is helping me a lot in my work.

    Reply
    • Mukesh Otwani says

      December 20, 2015 at 11:55 PM

      Hi Jyothi,

      Thanks a lot. Keep visiting and share with others too 🙂

      Reply
  23. Vicky says

    November 9, 2015 at 12:01 PM

    Nice website, which software you used to create it.
    Wordpress or created from scratch?

    Reply
    • Mukesh Otwani says

      November 9, 2015 at 9:33 PM

      Hi Vicky,

      Thanks I am using wordpress theme.

      Reply
  24. snehita says

    September 25, 2015 at 9:33 AM

    can we do data driven testing with the help of AutoIT incase of fileupload. I want to upload different files for different test cases with selenium webdriver and AutoIT. Is there any chance to do it.

    Reply
    • Mukesh Otwani says

      September 25, 2015 at 10:46 AM

      Hi Snehita,

      Yes, we can do this through data-driven framework but before running you should know how to pass the parameters to AutoIT Script.

      We can parameterize AutoIT script and we can change file name at run time through excel data or static data as well.

      Follow below link which will help you to parameterize script

      Please try and let me know if you are facing any issue.

      Have a nice day 🙂

      Reply
      • Suresh says

        December 26, 2015 at 8:03 AM

        Hi Mukesh, Can drop the link on how to parametrize autoit script while running it from eclipse.

        Thanks in advance
        Suresh

        Reply
        • Mukesh Otwani says

          December 30, 2015 at 11:50 AM

          Hi Suresh,

          here you go

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

          Runtime.getRuntime().exec(“C:\\path-of-exefile\\dummy.exe “+” “+”C:\\file-to-upload\\1.PNG”);

          Reply
          • sureshkaperla says

            December 31, 2015 at 5:20 PM

            Hi Mukesh, Thanks for the provided information on this. I tried below command

            Runtime.getRuntime().exec(“C:/AutoITScripts/pdf.exe”+” “+”Opening pricing.csv”)

            and printed the values on the message box.

            Actual Output is as follows:

            $CmdLine[1] is Opening
            $CmdLine[2] is pricing.csv

            However since it is one value, expect the value “Opening pricing.csv” to get assigned to $CmdLine[1].

            Any comments on this as to why this value is getting splitted despite it is provided within the double quotes.

            Thanks in advance
            Suresh

          • Mukesh Otwani says

            January 2, 2016 at 10:35 PM

            Hi Suresh,

            Path does not contain any spaces so while giving path for a specific file make sure it does not contain any spaces.

  25. Thao says

    September 22, 2015 at 2:43 PM

    bookmarked!!, I really like yojr website!

    Reply
    • Mukesh Otwani says

      September 22, 2015 at 8:26 PM

      Thank you Thao 🙂

      Reply
  26. Mukesh Otwani says

    September 19, 2015 at 7:42 PM

    Hi,

    Thanks a ton. I am glad that you liked above post 🙂

    Reply
  27. You Could Check Here says

    September 19, 2015 at 9:01 AM

    Usually I do not learn article on blogs, however I would like to
    say that this write-up very forced me to take a look at and do so!
    Your writing style has been surprised me. Thanks, very great post.

    Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?