[Selenium+Java] How to Upload & Download a File using Selenium Webdriver

來源:互聯網
上載者:User

標籤:base   exe   except   attribute   check   dialog   member   int   misc   

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

 

Uploading Files

For this section, we will use 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 is done by simply using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded.

Let‘s say we wish to upload the file "C:\newhtml.html". Our WebDriver code should be 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 be able to upload the file successfully and you should get a message similar to this.

Remember following two 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 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 from here 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 be 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 will persist in Eclipse and the error messages will not be as informative. Best to first get wget working using command line. If it works in command line it will definitely work in Eclipse.

In our example, as show in step 3, there is a problem writing into 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 will use WebDriver and wget to download a popular chat software called Yahoo Messenger. Our base URL shall be http://demo.guru99.com/test/yahoo.html.

Step 1

Import the "java.io.IOException" package because we will have to catch an 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 from 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 that the Yahoo Messenger installer was successfully downloaded there.

 

Summary

  • Uploading files in WebDriver is done by simply using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded.
  • WebDriver cannot automate downloading of files on its own.
  • The easiest way to download files using WebDriver is to use Wget.

[Selenium+Java] How to Upload & Download a File using Selenium Webdriver

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.