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

7

25

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

Eloyelreath answered 7/1, 2015 at 16:36 Comment(0)
O
33

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. :)

Obliteration answered 8/1, 2015 at 6:52 Comment(7)
Its working on Firefox now but how to do it while using Chome driver?Eloyelreath
Can i use AutoIT for this purpose? Is it mandatory to use AutoIT with TestNG or i can use it with selenium webdriver aloneEloyelreath
AutoIT till far is the best way. The prob wit Robot is that u need to freeze ur system while its executing. AutoIT removes that issue, but while using autoIT remember system should not goto suspension.Obliteration
and autoIt with testng or selenium webdriver? AutoIt is used as a Process call, so it has no relation with testng or selenium.Obliteration
Perhaps you want to use File.separator for the slashes in the path?Bradfield
This works for me. Thanks! Just one note: for more recent updates new ChromeDriver(DesiredCapabilities) is deprecated, but it works well just using new ChromeDriver(ChromeOptions) though lose SSL Cert definition.Avivah
Is there a way to change download path while on current session, similar to how you click Chrome Settings->Download ? The answer I saw always incur building new option + new driver + get a whole new session . I would wish not to close the current session, since my folder separation based on each item in a drop-down list and there's no need to reload a new page. There are thousands of items in that drop-down; the accepted method means closing and loading the page thousands times.Brookbrooke
N
7

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);
    
Nod answered 24/10, 2016 at 11:35 Comment(0)
I
5

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.

Iconology answered 20/5, 2016 at 20:43 Comment(0)
G
0

You partially answered your own question:

the problem where i am stuck is after getting the os window

Selenium is a browser automation tool - os window is not a browser! You will need to use something else. There are many choices, depending on your needs: Sikuli, Robot, AutoIt, ...

Gimcrack answered 7/1, 2015 at 19:17 Comment(1)
Is it possible to change the target folder location using Robot ?Eloyelreath
K
0

Use the same Robot class and press enter to select the "Save" in the Windows dialog box.

robo.keyPress(KeyEvent.VK_ENTER); robo.keyRelease(KeyEvent.VK_ENTER);

if you need to rename it copy the file name in the clipboard and pass like below

StringSelection file = new StringSelection("D:\\image.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(file, null);

Robot rb = new Robot();

rb.setAutoDelay(2000); // Similar to thread.sleep

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);

rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyRelease(KeyEvent.VK_V);

rb.setAutoDelay(2000);

rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
Kesley answered 10/5, 2017 at 7:6 Comment(0)
W
-1

Probably not the best solution but you could try using sikuli api to confirm the save for the box that shows up.

The save as box is an OS window.

Walcott answered 7/1, 2015 at 16:48 Comment(1)
Don't over engineer you test scriptAristocracy
M
-1

For Chrome, it will works

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);
WebDriver driver = new ChromeDriver(options);
Manicurist answered 22/8, 2018 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.