当前位置: 首页 > 知识库问答 >
问题:

如何使用Java/Selenium更改chrome的默认下载位置

程枫
2023-03-14

我试图改变chrome首选项使用下面的代码,但我不能这样做。我需要使用Java/Selenium更改chrome中文件的下载位置。我尝试了2-3个代码片段如下所述,但没有一个对我有效。

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);
Map<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();
Map<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
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);
options.addArguments("--disable-extensions");
caps.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(caps);

共有1个答案

奚曦哲
2023-03-14

我也面临着同样的问题。但是下面的代码帮助我解决了问题,并且工作得很好。

需要记住的要点:-

1)更改chrome驱动程序路径。

System.setProperty("webdriver.chrome.driver", "/path/to/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();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOptions("prefs", chromePrefs);
options.addArguments("--test-type");

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);

WebDriver driver = new ChromeDriver(cap);

driver.manage().window().maximize();
driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/");
driver.findElement(By.linkText("smilechart.xls")).click();
 类似资料:
  • 我想问一下,是否有人试图使用selenium 3.X更改微软Edge Chromium驱动程序的默认下载位置。在Chrome浏览器上,我们可以使用类似这样的内容 信息:Microsoft Edge浏览器版本:80.0.361.66(官方版本)(64位) 预先感谢

  • 问题内容: 请找到以下带有镶边功能的代码。实际上,浏览器没有将文件下载到指定的路径。 问题答案: 对于Chromedriver,请尝试: 注意:-在Windows中,您需要使用\\作为路径,而如果您使用linux或mac,则使用// 希望这可以帮助。:)

  • 问题内容: 在这篇文章中,我将看到为Chrome和Firefox设置下载目录的解决方案,以及 如何在使用chrome驱动程序/ firefox驱动程序时更改Webdriver中的文件下载位置 这些对我来说很完美(即已被接受的答案),但是在Internet周围搜索时找不到任何有关使用Internet Explorer 11的信息。有人知道我在哪里可以找到此信息吗? 问题答案: 根据与Internet

  • 问题内容: 我正在尝试使用pip安装Pandas,但遇到了一些麻烦。我刚运行成功下载了熊猫。但是,它没有下载到我想要的位置。这是我使用时看到的内容: 这样就安装好了。但是当我创建一个新的Python项目并在下面搜索熊猫时,我感到很困惑,因为它没有出现。但是,我过去下载的其他一些软件包的确出现了,所以我尝试看看它们在哪里。运行(我可以毫无问题地导入)产生: 这是在完全不同的目录中。为了确认我的错误,

  • 我正在尝试使用pip安装Pandas,但遇到了一些问题。我刚刚运行了,成功下载了pandas。但是,它没有下载到我想要的位置。以下是我使用时看到的内容: 所以它被安装了。但是当我创建一个新的Python项目并在System Libs/lib/python下搜索熊猫时,我感到困惑,因为它没有显示出来。然而,我过去下载的其他一些包确实出现了,所以我试着看看它们在哪里。运行(我可以毫无问题地导入)生成: