[Selenium+java] How to Upload & Download a File using Selenium webdriver

Source: Internet
Author: User
Tags guru99



Original source:https://www.guru99.com/upload-download-file-selenium-webdriver.html





Uploading Files


For this section, we'll use the Http://demo.guru99.com/test/upload/as our test application. This site easily allows any visitor to upload files without requiring them to sign up.



Uploading files in Webdriver are done by simply using the SendKeys () method on the File-select input field to enter the PAT H to the file to is uploaded.






Let's say we wish to upload the file "C:\newhtml.html". Our webdriver code should is like the one shown below.


package newproject;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PG9 {
    public static void main(String[] args) {
        System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
        String baseUrl = "http://demo.guru99.com/test/upload/";
        WebDriver driver = new FirefoxDriver();

        driver.get(baseUrl);
        WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));

        // enter the file path onto the file-selection input field
        uploadElement.sendKeys("C:\\newhtml.html");

        // check the "I accept the terms of service" check box
        driver.findElement(By.id("terms")).click();

        // click the "UploadFile" button
        driver.findElement(By.name("send")).click();
        }
}


After running this script, you should is able to upload the file successfully and we should get a message similar to this .






Remember following and things when uploading files in Webdriver


    1. There is no need to simulate the clicking of the "Browse" button. Webdriver automatically enters the file path onto the file-selection text box of the <input type= "file" > Element
    2. When setting the file path in your Java IDE, use the proper escape character for the Back-slash.




Downloading Files


Webdriver have no capability to access the Download dialog boxes presented by browsers when do click on a Downloa D 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'll 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 from where and place it in the Wget folder you created from the step above.






Step 2: Open Run by pressing Windows key + "R"; Type in "cmd & click OK






Type in the command "CD/" to move to the root directory






Step 3: Type in the command to check whether the given setup is working


CMD/C c:\\wget\\wget.exe-p C:--no-check-certificate Http://demo.guru99.com/selenium/msgr11us.exe





There seems to is an issue writing into C Drive.



Step 4: You need to debug the wget errors in command line before you execute the code using Selenium webdriver. These errors would persist in Eclipse and the error messages would not be as informative. Best-to-first get wget working using command line. If it works in command line it'll definitely work in Eclipse.



In we example, as show in step 3, there is a problem writing to C Drive. Let's change the download location to D drive and check results.


CMD/C c:\\wget\\wget.exe-p D:--no-check-certificate http://demo.guru99.com/selenium/msgr11us.exe





Messenger was downloaded successfully.



Before you proceed further don ' t forget to delete the downloaded file


Using Webdriver and Wget


In the following example, we'll use Webdriver and wget to download a popular chat software called Yahoo Messenger. Our base URL shall is http://demo.guru99.com/test/yahoo.html.






Step 1



Import the "java.io.IOException" package because we'll have to catch a ioexception later in Step 4.






Step 2



Use GetAttribute () 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 to our webdriver code.






To sum it all up, your webdriver code could look like the one shown below.


package newproject;
import java.io.IOException;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PG8 {
    public static void main(String[] args) {
        
        System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
                String baseUrl = "http://demo.guru99.com/test/yahoo.html";
        WebDriver driver = new FirefoxDriver();

        driver.get(baseUrl);
        WebElement downloadButton = driver.findElement(By
        .id("messenger-download"));
        String sourceLocation = downloadButton.getAttribute("href");
        String wget_command = "cmd /c C:\\Wget\\wget.exe -P D: --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) {
        System.out.println(ex.toString());
        }
        driver.close();
        }
        
}


After executing this code, check your D drive and verify so the Yahoo Messenger installer was successfully downloaded th Ere.









Summary


    • Uploading files in Webdriver are done by simply using the SendKeys () method on the File-select input field to enter the PAT H to the file to is uploaded.
    • Webdriver cannot automate downloading of files on its own.
    • The easiest-download files using Webdriver are to use Wget.


[Selenium+java] How to Upload & Download a File using Selenium webdriver


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.