- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to download any file and save it to the desired location using Selenium Webdriver?
We can download any file and save it to the desired location with Selenium. This can be done by creating an instance of the FirefoxOptions class. Then with the help of the addPreference method, we have to set the browser preferences.
We shall also specify the path where the file has to be downloaded with the help of the addPreference method.
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import java.util.concurrent.TimeUnit; public class FileDwnload{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); // create object of FirefoxOptions class FirefoxOptions profile = new FirefoxOptions(); // adding browser preferences with addPreference method profile.addPreference("browser.download.folderList", 2); // location of downloaded file profile.addPreference("browser.download.dir", "C:\Users\ghs6kor\Documents\Download"); profile.addPreference("browser.helperApps.neverAsk.openFile", "text/csv,application/x-msexcel,application/excel," + "application/x-excel,application/vnd.ms-excel," + "image/png,image/jpeg,text/html,text/plain," + "application/msword,application/xml"); profile.addPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/x-msexcel," + "application/excel," + "application/x-excel," +"application/vnd.ms- excel,image/png,image/jpeg,text/html," +"text/plain,application/msword,application/xml"); profile.addPreference("browser.helperApps.alwaysAsk.force", false); profile.addPreference ("browser.download.manager.alertOnEXEOpen", false); profile.addPreference("browser.download.manager.focusWhenStarting", false); profile.addPreference("browser.download.manager.useWindow", false); profile.addPreference("browser.download.manager.showAlertOnComplete", false); profile.addPreference("browser.download.manager.closeWhenDone", false); // connecting browser options to webdriver WebDriver driver = new FirefoxDriver(profile); driver.get("https://the-internet.herokuapp.com/download"); //maximize window driver.manage().window().maximize(); // identify element and start download driver.findElement(By.linkText("xls-sample1.xls")).click(); } }
Output
Also, the file gets downloaded at the specified location.
- Related Articles
- How to Download & Install Selenium WebDriver?
- How to save and load cookies using Python Selenium WebDriver?
- How to handle windows file upload using Selenium WebDriver?
- How to extract text from a web page using Selenium and save it as a text file?
- How to use chrome webdriver in Selenium to download files in Python?
- How to download a video using a URL and save it in an photo album using Swift?
- Downloading file to specified location with Selenium and python.
- File Upload using Selenium WebDriver and Java Robot Class.
- How can I download Microsoft WebDriver/Edge Driver to use with Selenium?
- How to download a file using Node.js?
- Access to file download dialog in Firefox in Selenium.
- How can I download a file on a click event using selenium?
- How to Verify Tooltip using Selenium WebDriver?
- How to upload files using Selenium Webdriver?
- How to obtain the page title using Selenium webdriver?

Advertisements