How to Download a File Using Selenium Webdriver Java
  Selenium is an 
open-source web-based automation tool that is
    implemented using a web driver. We will be using geckodriver because Selenium 3 enables
    geckodriver as the default WebDriver implementation for Firefox.
Pre-requisites:
- geckodriver.exe
- maven dependency selenium
 <dependency>
    <groupid>org.seleniumhq.selenium</groupid>
	<artifactid>selenium-java</artifactid>  
	<version>4.1.1</version> 
 </dependency>
Steps:
    The following 4 steps will automate the process:
  
  
  - 
      Set webdriver.gecko.driverand its'pathas a system property.
- Set the firefox diver and browse to the website link.
- 
      Get the file-download buttonas a web element usingwebdriver.findElement()function and css selector. 
- Use webdriver to click the webelement. This will download the file.
    Let’s see all the above steps in the code. We will use
        
webelement.click() function to click the download button that will start downloading the file.
  import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class DownloadFile {
    public static String GECKODRIVER_PATH = "F:\\WORK\\SeleniumShortTasks\\DownloadFile\\src\\main\\resources\\geckodriver.exe";
    public static void main(String[] args) {
        //set firefox webdriver
        System.setProperty("webdriver.gecko.driver", GECKODRIVER_PATH);
        WebDriver driver = new FirefoxDriver();
        //get the firefox browser & Browse the Website for file Download
        String siteLink = "https://sourceforge.net/projects/libreoffice/";
        driver.get(siteLink);
        //Get Download Button
        WebElement downloadButton = driver.findElement(By.cssSelector("a.button.download.big-text.green"));
        downloadButton.click();
    }
}
Output:
- source code
- geckdriver.exe

Comments
Post a Comment