How to change the default download directory with IE (Internet Explorer 11)
Asked Answered
H

4

8

In this post I see the solutions for setting the download directories for Chrome and Firefox how to change file download location in Webdriver while using chrome driver/firefox driver

These worked perfect for me (the accepted answer that is), however searching around I cannot find any information on doing this with Internet Explorer 11. Does anyone know where I can find this information?

Hyperostosis answered 26/3, 2016 at 21:34 Comment(0)
S
6

According to this answer from Jim Evans, who is heavily involved with WebDriver for Internet Explorer, this isn't possible:

Internet Explorer doesn't use profiles. It's a limitation of the browser itself, not the IE driver. As such, there is no way to automatically download files to a specified location with Internet Explorer.

Also:

As far as I am aware, there is no difference between Microsoft Edge and Internet Explorer with respect to using "profiles." The profile is still tied to the logged-in user account in Windows.

So, in a sense, the directory already is specified. The point is that you can't override it via WebDriver.

Sherrie answered 26/3, 2016 at 21:57 Comment(1)
Thanks Andrew! Not the news I was hoping for, but ok. Good to know.Hyperostosis
P
4

It's not possible through the driver, but you can define the location with this registry key:

HKCU\Software\Microsoft\Internet Explorer\Main\Default Download Directory
Primateship answered 26/3, 2016 at 23:7 Comment(3)
Thanks Florent! I will explore this approach, however I do not have a lot of faith in this working for me.Hyperostosis
Note that there is also a way to setup IE11 to automatically download a file to a specific location, but that was not your question.Primateship
Hello, is there anyway to define this through JavaScript? I have tried to find a way to update registry keys through Java but haven't found anything. Thanks!Talaria
M
0

I am changing default directory by changing REGEDIT.

String path = "\"C:\\Test\"";
String cmd1 = "REG ADD \"HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main\" /F /V \"Default Download Directory\" /T REG_SZ /D "+ path;

try {
    Runtime.getRuntime().exec(cmd1);
} catch (Exception e) {
    System.out.println("Coulnd't change the registry for default directory for IE");
}
Minardi answered 26/2, 2018 at 3:52 Comment(1)
I am looking for something like this but this gives me a bunch of syntax errorsTalaria
K
0
package auto.Selenium;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class SetDownloadPath {

    Robot r;

    @Test
    public void setPath() {

        System.setProperty("webdriver.ie.driver",
                "C:\\Users\\drivers\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        capabilities.setCapability("requireWindowFocus", true);
        capabilities.setCapability("nativeEvents", false);
        capabilities.setCapability("unexpectedAlertBehaviour", "accept");
        capabilities.setCapability("ignoreProtectedModeSetting", true);
        capabilities.setCapability("disable-popup-blocking", true);
        capabilities.setCapability("enablePersistentHover", true);
        capabilities.setCapability("ignoreZoomSetting", true);
        capabilities.setCapability("EnableNativeEvents", false);
        capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
        capabilities.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);

        WebDriver driver = new InternetExplorerDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().deleteAllCookies();
        driver.get("file:///C:\\Users\\ToBeSaved.pdf");
        String defaultPath = "C:\\Users\\DefaultSavedLocation";
        StringSelection stringSelection = new StringSelection(defaultPath);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, stringSelection);

        try {
            Thread.sleep(4000);
            r = new Robot();

            // Open the Pop Up
            r.keyPress(KeyEvent.VK_CONTROL);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_CONTROL);
            Thread.sleep(4000);
            // Reach to the URL bar
            for (int i = 0; i < 6; i++) {
                r.keyPress(KeyEvent.VK_TAB);
                r.keyRelease(KeyEvent.VK_TAB);
            }
            // Paste the copied default ULR
            r.keyPress(KeyEvent.VK_ENTER);
            Thread.sleep(4000);
            r.keyPress(KeyEvent.VK_CONTROL);
            r.keyPress(KeyEvent.VK_V);
            r.keyRelease(KeyEvent.VK_V);
            r.keyRelease(KeyEvent.VK_CONTROL);
            // Save the file
            for (int i = 0; i < 5; i++) {
                Thread.sleep(2000);
                r.keyPress(KeyEvent.VK_ENTER);
            }

        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Komsomolsk answered 14/1, 2019 at 8:7 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Seismism

© 2022 - 2024 — McMap. All rights reserved.