how to change file download location in Webdriver while using chrome driver/firefox driver

84,788

Solution 1

There are two things that are going wrong in code.

For Firefox: You need to set

profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\");

not to

profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\pic.jpeg");

secondly, you are setting preference browser.download.folderlist, it is browser.download.folderList (L caps in folderList).

Once you have achieved this both, you can use then your Robot class to perform desired operations.

For Chromedriver try out with:

String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);

Hope this helps. :)

Solution 2

For Chrome Browser:

Even you can disable the windows dialogue (Save As Dialogue) with the following code snippet. You need to do following settins in the chromedriver preferences:

  • turn off the download prompt if it appears
  • set the default directory to download the file
  • If PDF view plugin is enabled which opens the PDF file in browser, you can disable that so that download can start automatically
  • Accept any certificate in browser

    String downloadFilepath = "/path/to/download/directory/";
    Map<String, Object> preferences = new Hashtable<String, Object>();
    preferences.put("profile.default_content_settings.popups", 0);
    preferences.put("download.prompt_for_download", "false");
    preferences.put("download.default_directory", downloadFilepath);
    
    // disable flash and the PDF viewer
    preferences.put("plugins.plugins_disabled", new String[]{
        "Adobe Flash Player", "Chrome PDF Viewer"});
    
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", preferences);
    
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(capabilities);
    

Solution 3

I spent a lot of time to investigate how to download pdf file in firefox browser without Save As popup appearance. It migth be help someone.

After some local investigation, how to download pdf file in firefox without any Save As popup, I found the minimum required preference in firefox profile:

profile.setPreference("pdfjs.disabled", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

Of course you can add some additional preferences.

It works in Firefox 45-46 versions.

Share:
84,788
Shantanu Nandan
Author by

Shantanu Nandan

Updated on July 26, 2022

Comments

  • Shantanu Nandan
    Shantanu Nandan almost 2 years

    I am trying to save an image by using save as option inside a specific folder. I found a way by which I am able to right click on the image which I want to save using save as option. But the problem where I am stuck is after getting the os window which asks where to save the file I am not able to send the desired location because I don't know how to do it. I went through the similar questions asked on this forum but non of them helped so far.

    Code is-

    For Firefox-

    public class practice {
    
     public void pic() throws AWTException{
         WebDriver driver;
    
         //Proxy Setting     
            FirefoxProfile profile = new FirefoxProfile();
            profile.setAssumeUntrustedCertificateIssuer(false);
            profile.setEnableNativeEvents(false);
            profile.setPreference("network.proxy.type", 1);
            profile.setPreference("network.proxy.http", "localHost");
            profile.setPreference("newtwork.proxy.http_port",3128);
    
            //Download setting
            profile.setPreference("browser.download.folderlist", 2);
            profile.setPreference("browser.helperapps.neverAsk.saveToDisk","jpeg");
            profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\pic.jpeg");
            driver = new FirefoxDriver(profile);
    
            driver.navigate().to("http://stackoverflow.com/users/2675355/shantanu");
            driver.findElement(By.xpath("//*[@id='large-user-info']/div[1]/div[1]/a/div/img"));
            Actions action = new Actions(driver);
            action.moveToElement(driver.findElement(By.xpath("//*[@id='large-user-info']/div[1]/div[1]/a/div/img"))).perform();
            action.contextClick().perform();
            Robot robo = new Robot();
            robo.keyPress(KeyEvent.VK_V);
            robo.keyRelease(KeyEvent.VK_V);
        // Here I am getting the os window but don't know how to send the desired location
        }//method   
    }//class
    

    For chrome-

    public class practice {
       public void s() throws AWTException{
            WebDriver driver;   
            System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Desktop\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.navigate().to("http://stackoverflow.com/users/2675355/shantanu");
            driver.findElement(By.xpath("//*[@id='large-user-info']/div[1]/div[1]/a/div/img"));
            Actions action = new Actions(driver);
            action.moveToElement(driver.findElement(By.xpath("//*[@id='large-user-info']/div[1]/div[1]/a/div/img"))).perform();
            action.contextClick().perform();
            Robot robo = new Robot();
            robo.keyPress(KeyEvent.VK_V);
            robo.keyRelease(KeyEvent.VK_V);
            // Here I am getting the os window but don't know how to send the desired location
       }
     }
    

    This is the pop up window where I am stuck