简体   繁体   中英

How do I download a file using java Selenium WebDriver?

I'm using Selenium 2.21.0 with Java 6. How do I use the Selenium WebDriver API to download a file on a web page? That is, there is a link that causes the download of an Excel file to start. I would like to know how to initiate that download, determine when its finished, and then figure out where the file got downloaded to on my local system.

Once you click on any link to download a file, it depends on browsers behaviour like Chrome Behaviour: It will start downloading the file by default, as soon as the user click on the link of any file. IE Behaviour: IE displays a bar at the bottom of the window and displays the option to Save or Cancel the file download. FireFox Behaviour: This will display an dialog box window and displays the option to Save or Cancel the file download. So this can be achieved with the help of FireFox Profile. And Before downloading any file you have to pass the MIME type of the file to FireFox Profile. Some of the commonly used MIME types are: Text File (.txt) – text/plain PDF File (.pdf) – application/pdf CSV File (.csv) – text/csv MS Excel File (.xlsx) – application/vnd.openxmlformats-officedocument.spreadsheetml.sheet MS word File (.docx) – application/vnd.openxmlformats-officedocument.wordprocessingml.document

HERE IS THE CODE:

    import org.openqa.selenium.By;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;

    public class DownloadFiles {

    public static void main(String[] args) throws InterruptedException {
    //Create FireFox Profile object
    FirefoxProfile p = new FirefoxProfile();

    //Set Location to store files after downloading.
    profile.setPreference("browser.download.folderList", 2);

   //Set preference not to file confirmation dialogue
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
        "application/vnd.openxmlformats-
        officedocument.spreadsheetml.sheet"); 

  // Specify the local system path where to download
     p.setPreference("browser.download.dir", "D:\\downloads");
  // Pass Profile parameters in Firefox browser
    FirefoxDriver driver = new FirefoxDriver(profile);  

    // Open APP to download application
    driver.get("http://url");

    // Click to download 
    driver.findElement(By.xpath("//html[@attribute='value']")).click();

    Thread.sleep(5000);

    driver.close();

Hope it will solve your queries. Happy coding.

I am specifically focusing of the Firefox browser you can use where the download option comes with pop up option when any download link is been click by the visitor. It is shows two buttons and two radio button that gives us the option of save and open the file directly without downloading it afterwards if we find the file is useful we can download it explicitly though the download icon on the above toolbar of the Firefox browser. So you can perform the following steps

1) Clicking on the download link

WebDriver driver = new FirefoxDriver();

driver.findElement(By.linkText(“somelink”)).click();

The above code can help the WebDriver to identify the object and perform the action which is mentioned

2) Once the download link is clicked the firefox browser will pop up a download dialog box with multiple option Like Save radio button, open radio button, ok button, cancel button inorder to use this You can use the Robot class or the Keys in the WebDriver Like

Robot r = new Robot();


r.KeyPress(KeyEvent.VK_TAB); 

you can use as many time the above code to press the tab button

r.KeyRelease(KeyEvent.VK_TAB);

you have to release the pressed key

lastly perform the enter

r.KeyPress(KeyEvent.VK_ENTER);

That's it it will help you to download the object when the download link is pressed

Hope it will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM