如何在selenium Chrome功能中设置默认下载目录?

问题描述 投票:12回答:5

请使用chrome功能查找以下代码。实际上,浏览器没有将文件下载到指定的路径。

private static DesiredCapabilities getChromeCapabilities() throws Exception {

    String chromePath = BrowserUtil.class.getResource("/Browserdrivers/chromedriver.exe").getPath();
    System.setProperty("webdriver.chrome.driver", chromePath);
    String downloadFilepath = "C:\\TestDownloads";
    ChromeOptions options = new ChromeOptions();
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    options.setExperimentalOption("prefs", chromePrefs);
    options.addArguments("--test-type");
    options.addArguments("start-maximized", "disable-popup-blocking");

    DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
    setProxy(chromeCapabilities);
    chromeCapabilities.setPlatform(Platform.WINDOWS);
    chromeCapabilities.setCapability("name", MDC.get("testname"));
    chromeCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
    return chromeCapabilities;
}
java selenium selenium-webdriver selenium-chromedriver
5个回答
21
投票

对于Chromedriver试用:

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);

注意: - 在Windows中,你需要使用\\作为路径,如果你使用的是linux或mac,那么使用//

希望这可以帮助。 :)


7
投票

ans帮助我在windows(https://bugs.chromium.org/p/chromedriver/issues/detail?id=783)上解决这个问题。

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory",  System.getProperty("user.dir")+ File.separator + "externalFiles" + File.separator + "downloadFiles");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
ChromeDriver driver = new ChromeDriver(options);

2
投票

对于Chrome驱动程序,以下代码适用于我

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);

1
投票

对于看到此页面的Python用户 - 这是我在Python Selenium中设置下载目录的方式(这只是Shubham接受的答案的Python版本):

def newChromeBrowser(headless=True, downloadPath=None):
    """ Helper function that creates a new Selenium browser """
    options = webdriver.ChromeOptions()
    if headless:
        options.add_argument('headless')
    if downloadPath is not None:
        prefs = {}
        os.makedirs(downloadPath)
        prefs["profile.default_content_settings.popups"]=0
        prefs["download.default_directory"]=downloadPath
        options.add_experimental_option("prefs", prefs)
    browser = webdriver.Chrome(chrome_options=options, executable_path=CHROMEDRIVER_PATH)
    return browser

0
投票

为了使它更干净和简单,我开发了一个library,它允许您生成一个ChromeOptions对象,其中包含您的下载文件夹在一行。例如,要定义“/ tmp / downloads”,请使用:

private SeleniumDownloadKPI seleniumDownloadKPI;

@BeforeEach
void setUpTest() {

    // New instance of SeleniumDownloadKPI with given download folder.
    seleniumDownloadKPI =
         new SeleniumDownloadKPI("/tmp/downloads");
    ChromeOptions chromeOptions =
            seleniumDownloadKPI.generateDownloadFolderCapability();
    driver = new ChromeDriver(chromeOptions);

library还包含允许接收下载KPI并执行断言的方法。

© www.soinside.com 2019 - 2024. All rights reserved.