Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
334 views

How To Upload & Download A File Using Selenium Webdriver

fghfgh

Uploaded by

Interact people
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
334 views

How To Upload & Download A File Using Selenium Webdriver

fghfgh

Uploaded by

Interact people
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

12/13/2018 How to Upload & Download a File using Selenium Webdriver

(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

How to Upload & Download a File using Selenium


Webdriver
In this tutorial, we will learn How to deal with file uploads and downloads.

Uploading Files
For this section, we will use http://demo.guru99.com/test/upload/
(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.

(/images/2-

2017/030717_1029_KeyboardMou10.png)
Handle File upload popup in Selenium Webdriver

handle file upload popup in selenium webdriver


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

https://www.guru99.com/upload-download-file-selenium-webdriver.html 1/11
12/13/2018 How to Upload & Download a File using Selenium Webdriver
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.

(/images/2-

2017/030717_1029_KeyboardMou11.png)

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.

(/images/image058.png)

Downloading Files
https://www.guru99.com/upload-download-file-selenium-webdriver.html 2/11
WebDriver
12/13/2018 has no capability to How
access the
to Upload Download
& Download dialog
a File using Seleniumboxes
Webdriverpresented 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.

Se ng up Wget
Step 1: In your C Drive, create a new folder and name it as "Wget".

Download wget.exe from here (https://eternallybored.org/misc/wget/) and Place it in the


Wget folder you created from the step above.

(/images/2-2017/030717_1029_KeyboardMou13.png)

Step 2: Open Run by pressing windows key + "R" ; type in "cmd & click ok

(/images/2-

2017/030717_1029_KeyboardMou14.png)

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

https://www.guru99.com/upload-download-file-selenium-webdriver.html 3/11
12/13/2018 How to Upload & Download a File using Selenium Webdriver

(/images/2-2017/030717_1029_KeyboardMou15.png)

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/msgr


11us.exe

(/images/2-2017/030717_1029_KeyboardMou16.png)

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/msgr


11us.exe

https://www.guru99.com/upload-download-file-selenium-webdriver.html 4/11
12/13/2018 How to Upload & Download a File using Selenium Webdriver

(/images/2-2017/030717_1029_KeyboardMou17.png)

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 (http://demo.guru99.com/test/yahoo.html).

(/images/image064.png)

Step 1

Import the "java.io.IOException" package because we will have to catch an IOException later
in Step 4.

(/images/image065.png)

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".

(/images/image066.png)

Step 3
https://www.guru99.com/upload-download-file-selenium-webdriver.html 5/11
Set-up
12/13/2018 the syntax for wget using the following
How to command.
Upload & Download a File using Selenium Webdriver

(/images/image067.png)

Step 4

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

(/images/image068.png)

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

https://www.guru99.com/upload-download-file-selenium-webdriver.html 6/11
12/13/2018 How to Upload & Download a File using Selenium Webdriver
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 " + s
ourceLocation;

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.

https://www.guru99.com/upload-download-file-selenium-webdriver.html 7/11
12/13/2018 How to Upload & Download a File using Selenium Webdriver

(/images/2-

2017/030717_1029_KeyboardMou23.png)

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.

 Prev (/keyboard-mouse-events-files-webdriver.html) Report a Bug

Next  (/xpath-selenium.html)

YOU MIGHT LIKE:

SELENIUM SELENIUM SELENIUM

(/selenium-github.html) (/breakpoints-startpoints- (/click-on-image-in-


(/selenium- selenium.html) selenium.html)
github.html) (/breakpoints- (/click-on-image-in-
startpoints- selenium.html)
Upload Selenium Script to
GitHub selenium.html) How to Click on Image in
(/selenium-github.html) Breakpoint & Start Point in Selenium Webdriver
Selenium IDE (/click-on-image-in-
(/breakpoints-startpoints- selenium.html)
selenium.html)

SELENIUM SELENIUM
https://www.guru99.com/upload-download-file-selenium-webdriver.html SELENIUM 8/11
12/13/2018 How to Upload & Download a File using Selenium Webdriver
(/selenium-python.html) (/top-100-selenium- (/testng-tutorial.html)
(/selenium- interview-questions- (/testng-
python.html) answers.html) tutorial.html)
(/top-100-selenium-
How to Use Selenium with How to Execute Failed Test
Python: Complete Tutorial interview-questions- Cases in TestNG: Selenium
answers.html) WebDriver
(/selenium-python.html)
Top 100 Selenium Interview (/testng-tutorial.html)
Ques ons & Answers
(/top-100-selenium-
interview-questions-
answers.html)

Selenium Tutorials
1) Introduction (/introduction-to-selenium.html)

2) Intro WebDriver (/introduction-webdriver-comparison-selenium-rc.html)

3) Install Webdriver (/installing-selenium-webdriver.html)

4) First WebDriver Script (/first-webdriver-script.html)

5) Locators (/locators-in-selenium-ide.html)

6) Find Element Selenium (/find-element-selenium.html)

7) Forms & Webdriver (/accessing-forms-in-webdriver.html)

8) CheckBox & Radio Button WebDriver (/checkbox-and-radio-button-webdriver.html)

9) Click Image Webdriver (/click-on-image-in-selenium.html)

10) Selenium Webdriver DropDown (/select-option-dropdown-selenium-webdriver.html)

11) Links & Tables (/accessing-links-tables-selenium-webdriver.html)

12) Keyboard Mouse Events (/keyboard-mouse-events-files-webdriver.html)

13) Upload & Download File (/upload-download-file-selenium-webdriver.html)

14) XPath in Selenium (/xpath-selenium.html)

15) Alert & Popup handling (/alert-popup-handling-selenium.html)

16) Handle Web Table (/selenium-webtable.html)

17) Handling Dynamic Web Tables (/handling-dynamic-selenium-webdriver.html)

18) Desired Capabilities in Selenium (/desired-capabilities-selenium.html)

19) Verify Tooltip WebDriver (/verify-tooltip-selenium-webdriver.html)

20) Find Broken links (/find-broken-links-selenium-webdriver.html)


https://www.guru99.com/upload-download-file-selenium-webdriver.html 9/11
12/13/2018 How to Upload & Download a File using Selenium Webdriver
21) Gecko (Marionette) Driver (/gecko-marionette-driver-selenium.html)

22) Install TestNG in Eclipse (/install-testng-in-eclipse.html)

23) Selenium & TestNG (/all-about-testng-and-selenium.html)

24) Introduction to TestNG Groups (/introduction-testng-groups.html)

25) Test Case Priority in TestNG (/test-case-priority-testng.html)

2) Install IDE & FireBug (/install-selenuim-ide.html)

3) Introduction IDE (/introduction-selenuim-ide.html)

4) First Script (/first-selenium-test-script.html)

6) Enhancements (/enhancing-selenium-ide-script.html)

7) Variables, Echo, Alert, PopUp (/store-variables-handling-selenium-ide.html)

 (https://www.facebook.com/guru99com/) 
(https://twitter.com/guru99com) 
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)

(https://forms.aweber.com/form/46/724807646.htm)

About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)

Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)

Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)
https://www.guru99.com/upload-download-file-selenium-webdriver.html 10/11
12/13/2018 How to Upload & Download a File using Selenium Webdriver

Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)

© Copyright - Guru99 2018


Privacy Policy (/privacy-policy.html)

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

You might also like