如何使用IE更改默认下载目录(Internet Explorer 11)

时间:2016-03-26 21:34:50

标签: java selenium selenium-webdriver selenium-grid

在这篇文章中,我看到了为Chrome和Firefox设置下载目录的解决方案 how to change file download location in Webdriver while using chrome driver/firefox driver

这些对我来说非常完美(已接受的答案),但是我无法找到有关使用Internet Explorer 11执行此操作的任何信息。有谁知道我可以在哪里找到此信息?

3 个答案:

答案 0 :(得分:5)

根据来自Internet Explorer的WebDriver的Jim Evans的this answer,这是不可能的:

  

Internet Explorer不使用配置文件。这是一个限制   浏览器本身,而不是IE驱动程序。因此,没有办法   使用Internet自动将文件下载到指定位置   资源管理器。

此外:

  

据我所知,Microsoft Edge之间没有区别   和Internet Explorer一起使用"配置文件。"个人资料是   仍然与Windows中的登录用户帐户相关联。

因此,从某种意义上说,目录已经指定。关键是你不能通过WebDriver覆盖它。

答案 1 :(得分:4)

通过驱动程序无法实现,但您可以使用此注册表项定义位置:

HKCU\Software\Microsoft\Internet Explorer\Main\Default Download Directory

答案 2 :(得分: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();
        }
    }
}