Uploading and downloading in Selenium

Uploading and Downloading in Selenium is common activity in today’s web world. Like at the time of exam form filling you need to upload your photo or some other document. Same as in company on-boarding process, google drive upload etc. So how you will automate this uploading and downloading in Selenium?

Yes. By using selenium WebDriver we can achieve this.

File uploading can be automated in 3 ways. i.e.

            Sendkeys to handle file upload  <Link>

            AutoIT Script to handle file upload <Link>

            Jacob API to handle file upload <Link>

Sendkeys to handle file upload

File uploading is a very completed task to automate. But it will be very easy when you use sendKeys().

But how?

Yes, like text box, you can use this sendKeys() to upload file. See the below scenario.

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class UploadTesting {
    public static void main(String[] args) {
        String baseUrl = "http://demo.testinglpoint.com";
        WebDriver driver = new FirefoxDriver();

        WebElement uploadElement =        

Here sendKeys() will accept the file location of the system and load the file in corresponding field. Once it is uploaded submit to upload action will upload the same file in server.

Downloading Files in Selenium

In last section, we have understood how to upload file in selenium by passing path in sendKeys() method as parameter. Now we will check how we can achieve downloading features in Selenium?

To achieve download scenario, we need to proceed with below test step.

  • Go to web page
  • Click on the Download link
  • Click on the download

From the above test steps, you can achieve first 2 step easily in WebDriver. But the step 3 is not supported by Selenium WebDriver.

To perform the Download process in selenium we need to implement wget. wget will bypass the download dialog box using a separate program. Let’s understand what is wget.

wget is nothing but a command line program used to implement download process with selenium. Before implementing wget, we need to set-up little bit to perform download process in selenium.

Setting up the wget:

Step-1: Folder creation

Create a folder with name wget in your D drive.

Step-2: Download wget

After creation of wget folder in D drive, download wget from here and paste this file inside the wget folder you have created.

Uploading and downloading in Selenium

We are done with wget setup. Now we can proceed with implement the wget with WebDriver.

Integrate wget with WebDriver.

Step-1: Import the “java.io.IOException”

We need to import java.io.IOException because we will be working with file download process. It may trigger the IOException. We we need to handle this.

import java.io.IOException; 

Step-2: Get href value from the download link

To get the href value from the download link, we need to use getAttribute() and save the returned value as string.

WebElement downlaodLink = driver.findElement(By.id("ID of the downlaod link"));
String hrefValue = downloadLink.getAttribute("href");

Step-3: Set up the wget

We need to set up the wget by using the following command.

String wget_comd =  "cmd / c wget -P D: " + hrefValue 

-P is showing indicating the path of the wget in your drive location,

Step-4: Initiate the Download Process

We are done with the wget setup. Now we can proceed for download process implementation through below code.

Process exec = Runtime.getRuntime().exec(wget_command);
int exitVal = exec.waitFor();
System.out.println("Exit value: " + exitVal);
catch (InterruptedException | IOException ex) {

After executing this code you can see the downloaded files in your D:// drive.

Leave a Reply

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