Java 如何在 selenium Chrome 功能中设置默认下载目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34515328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set default download directory in selenium Chrome Capabilities?
提问by vini007
Please find the below code with the chrome capabilities. In fact the browser is not downloading the file to the specified path.
请找到以下带有 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;
}
采纳答案by Shubham Jain
For Chromedriver try out with:
对于 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);
Note:- In windows you need to use \\ for path while if you are using linux or mac then use //
注意:- 在 Windows 中,您需要使用 \\ 作为路径,而如果您使用的是 linux 或 mac,则使用 //
Hope this helps. :)
希望这可以帮助。:)
回答by Darshan Shah
The ans which help me to resolve this issue on windows (https://bugs.chromium.org/p/chromedriver/issues/detail?id=783).
帮助我在 Windows 上解决此问题的 ans ( 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);
回答by AutomatedOwl
To make it more clean and easy, I developed a librarywhich lets you generate a ChromeOptions object which contains your download folder in one line. For example, to define "/tmp/downloads", use:
为了使它更简洁,我开发了一个库,它允许您生成一个 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);
The libraryalso contains methods which allow to receive download KPI and perform assertion.
该库还包含允许接收下载 KPI 和执行断言的方法。
回答by Bachan Joseph
For Chrome driver, the below code is worked for me
对于 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);
回答by Luke
For Python users who see this page -- here's how I set the download directory in Python Selenium (this is just the Python version of Shubham's accepted answer):
对于看到此页面的 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