How to Upload File using Selenium WebDriver

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

Selenium is WebAutomation tool you can’t automate the File Upload functionality using Selenium directly because to upload the file, first you need to select the file from your local(Your Machine) then send it to the application.

But you can’t select the file using Selenium because Selenium can’t communicate with your window or Linux machine. So Now question is, How can you Automate the File Upload functionality using Selenium ?

 

To automate the File Upload functionality you need to use AutoIt with Selenium Scripts. First, you need to transfer the control to AutoIt and Using AutoIt tool you can Select the file (communicate with  the Operating System) and again transfer the control to Selenium Scripts to test the upload functionality.

We need to explicitly call the AutoIt script from our program. You can see, How to Install the AutoIt on Windows?

 

To test the file upload functionality, We need to call the AutoIt script after clicking on the upload button. Immediately after clicking on Upload button, the control should be transferred to AutoIt by the below statement which takes care of uploading the file.

Syntax:

 

Upload file using Selenium Example

In this example we will see, How can you upload the file using Selenium and test the file Upload functionality.

Let’s choose the web portal (https://smallpdf.com/jpg-to-pdf) this URL will open the web page. Which have the functionality to choose and Upload the file. On this webPage, we have the link ‘Choose file‘. We can click this link using Selenium Script but it will switch the control and open the window base popup to select the file from the local machine, which we can’t control using Selenium Script.
 
So Now we need to perform two tasks-

  1. Navigate to web page and click on ‘Choose file’ link using Selenium
  2. Select the file using AutoIt tool.

Upload file using Selenium

 

Selenium Part :

1. Launch the Browser.

2. Navigate to Target URL.

3. Find the element ‘Choose File’ & click it.

See the below-mentioned code –

The above code will open the Choose file Window base Popup. Now we need AutoIt Script to choose the file from local and communicate to Window base popup.

 

AutoIt Part :

I believe you have installed the AutoIt in your machine. Now we will see, How can you create AutoIt Script –

1. Go to any directory and perform Right-Click.

2. Navigate to ‘New‘. You will get the Option ‘AutoIt Script‘.

Create AutoIt Script

3. Click it. It will create the New file with extension ‘.au3‘. Rename your file and save it.

4. Now right click your newly created file. You will get the option ‘Edit Script‘, Click it.

5. You will get the AutoIt editor prompt.

AutoIt Prompt
 
This is the place where you can enter your script in AutoIt to upload the file.

Script –

Here within the ‘send command‘, I am providing my file path, which I want to upload.

Now save this file and execute this file after click on ‘Choose File‘  link. Now let’s see how can your execute this with Selenium Script to upload the file.

 
The above code will upload the file successfully.

Command –

 
Is responsible for executing my AutoIt script. Here FileUpload.exe is my AutoIt Script.

But if you observe we are passing ‘.exe’ file to the script. And the file we have just saved is with ‘.au3’ extension.

We actually need to generate the ‘.exe’ file by compiling it into a standalone executable.

 

How to compile AutoIt file ‘.au3’ to ‘.exe’ file

There are multiple ways to do that. But the simple way is using Right Click option. To do that, we will follow the below steps

Step-1: Navigate to the .au3 file that you wish to compile.
Step-2: Select the file and Right-click on it to access the pop-up menu.
Step-3: You will get an option to ‘Compile Script’. Click on ‘Compile Script.

File compile AutoIt
 

After performing above steps, it will generate a compiled file with the same filename with a .exe extension. Now pass this file to the test script. That’s It!!!!!

Hope this tutorial helps you. Please fell free to comment.

 

1 Comment
  1. Maron Menard 7:17 PM / September 22, 2016 - Reply

    Thanks … I was looking for this for long time ….

Leave a Reply