在 chrome 处更改没有弹出 window 的下载目录

change the download directory without the popup window at chrome

我正在使用 Selenium 和 Java 为 Chrome 浏览器编写测试。在某些时候我需要下载一个文件,我需要更改文件将被下载到的目录。问题是,当我使用下面的代码片段时,它会打开弹出窗口 window 而我不想要它:

String downloadFilepath = "download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downloadFilepath);
options.setExperimentalOption("prefs", chromePrefs);    

添加以下参数作为 chromeOptions :-

options.addArguments("disable-popup-blocking")

希望对你有所帮助..:)

你能试试这个吗?

String downloadFilepath = "download";
 Map<String, Object> prefs = new HashMap<String, Object>();
 prefs.put("download.default_directory", downloadFilepath);
 DesiredCapabilities caps = DesiredCapabilities.chrome();
 ChromeOptions options = new ChromeOptions();
 options.setExperimentalOption("prefs", prefs);
 caps.setCapability(ChromeOptions.CAPABILITY, options);