Monday, 20 February 2017

Download and validate file using webdriver,wget

Downloading Files

WebDriver has no capability to access the Download dialog boxes presented by browsers when you click on a download link or button. However, we can bypass these dialog boxes using a separate program called "wget".

What is Wget?

Wget is a small and easy-to-use command-line program used to automate downloads. Basically, we will access Wget from our WebDriver script to perform the download process.  

Setting up Wget

Step 1
In your C Drive, create a new folder and name it as "Wget".
Download wget.exe here and place it in the Wget folder you created from the step above.
Step 2
Bring up the System Properties window by pressing Win + Pause on your keyboard.
Click on "Advanced System Settings"

Click on the "Environment Variables".

Step 3
In the Environment Variables dialog box, in the "System variables" section, scroll down the list until you find "Path" and then double-click it.

Step 4
In the "Variable value" text box, add the new path "C:\Wget\". Just be sure that there is a semi-colon separating this new entry from the pre-existing values

Step 5
Click OK on all dialog boxes.
Launch the command prompt and type the command "wget". You should get the following response.

Using WebDriver and Wget

In the following example, we will use WebDriver and wget to download a popular chat software called Yahoo Messenger. Our base URL shall be

Step 1
Import the "" package because we will have to catch an IOException later in Step 4.
Step 2
Use getCssAttribute() to obtain the "href" value of the download link and save it as a String variable. In this case, we named the variable as "sourceLocation".

Step 3
Set-up the syntax for wget using the following command.

Step 4
Initiate the download process by calling wget from our WebDriver code.

To sum it all up, your WebDriver code could look like the one shown below.
package mypackage;


import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class myclass {

public static void main(String[] args) {
String baseUrl = "";
WebDriver driver = new FirefoxDriver();

WebElement downloadButton = driver.findElement(By
String sourceLocation = downloadButton.getAttribute("href");
String wget_command = "cmd /c wget -P c: --no-check-certificate " + sourceLocation;

try {
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, check your C drive and verify that the Yahoo Messenger installer was successfully downloaded there.

No comments:

Post a Comment

What is Wireframe ?

    A wireframe is a layout of a web page that demonstrates what interface elements will exist on key pages. It is a criti...