gpt4 book ai didi

java - 如何使用 java/selenium 更改 chrome 的默认下载位置

转载 作者:太空宇宙 更新时间:2023-11-04 12:14:00 25 4
gpt4 key购买 nike

我尝试使用下面的代码更改 chrome 首选项,但我无法做到。我需要使用 java/selenium 更改 chrome 中文件的下载位置。我尝试了下面提到的 2-3 个代码片段,但它们都不适合我。

代码片段-1

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

代码片段-2

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

代码片段3

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)更改chrome驱动程序路径。

2)将下载文件路径更改为您需要的位置。

代码:-

@测试

public void testDownload() 抛出异常 {

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

}

谢谢。

关于java - 如何使用 java/selenium 更改 chrome 的默认下载位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39591696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com