
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 can I download a file on a click event using selenium?
We can download a file on a click event in Selenium. Normally, on clicking on the download link, a pop-up comes with the file name along with the Open and Save option.
To download with a click event, an object of the FirefoxOptions class is to be created. Then with the addPreference method, we shall configure browser preferences.
We shall mention the location where the download is to be done along with the neverAsk.openFile and neverAsk.saveToDisk preferences are to be set.
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 FileDwnloadWithClick{ 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"); // 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-sample2.xls")).click(); } }
Output
Moreover, file gets downloaded at the mentioned location.
- Related Questions & Answers
- How can I trigger a JavaScript click event?
- How to download a file using Node.js?
- How to call a JavaScript function on a click event?
- How do I download Selenium RC?
- How to click on a link in Selenium?
- How to simulate a click event using jQuery?
- How to click on a hyper link using linkText in Selenium?
- How to click on a link using Selenium webdriver in Python.
- How can I verify Error Message on a webpage using Selenium Webdriver?
- How to click on <input type=file> across browsers using Selenium Webdriver?
- How to submit a form using jQuery click event?
- How to handle a link click event using jQuery?
- How to handle a double click event using jQuery?
- How to click a href link using Selenium?
- How to draw a dot on a canvas on a click event in Tkinter Python?
Advertisements