Download a file to a specific path using Selenium WebDriver

前端 未结 3 1391
春和景丽
春和景丽 2021-01-15 13:54

I need to download a file to a given location on a non-local machine. This is the normal flow of the web browser for which I would do this:

  • Go to website
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 14:52

    Use selenium webdriver

    Use firefox profile to download your files. This profile skip that dialogue box of firefox. In line:-

       pro.setPreference("browser.downLoad.folderList", 0);
    

    The value of browser.download.folderList can be set to either 0, 1, or 2. When set to 0, Firefox will save all files downloaded via the browser on the user's desktop. When set to 1, these downloads are stored in the Downloads folder. When set to 2, the location specified for the most recent download is utilized again.

    Firefox profile code that you need to implement :-

            FirefoxProfile pro=new FirefoxProfile();
            pro.setPreference("browser.downLoad.folderList", 0);
            pro.setPreference("browser.helperApps.neverAsk.saveToDisk", "Applications/zip");
            WebDriver driver=new FirefoxDriver(pro);
            driver.get("http://selenium-release.storage.googleapis.com/2.47/selenium-java-2.47.1.zip");
    

    Hope it will help you :)

提交回复
热议问题