How To Upload & Download A File Using Selenium Webdriver
How To Upload & Download A File Using Selenium Webdriver
(https://www.guru99.com/)
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
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"));
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)
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".
(/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)
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
(/images/2-2017/030717_1029_KeyboardMou16.png)
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.
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)
Before you proceed further don't forget to delete the downloaded file
(/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.
Next (/xpath-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)
5) Locators (/locators-in-selenium-ide.html)
6) Enhancements (/enhancing-selenium-ide-script.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)
https://www.guru99.com/upload-download-file-selenium-webdriver.html 11/11