I use the suggested methods for setting profile and folder path for downloads shown on the docs
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "C:\\testDownloads");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\seleniumChromeProfile");
options.setExperimentalOption("prefs", prefs);
ChromeDriver driver= new ChromeDriver(options);
And it works. With the Preferences
file open in notepad++ I can see that when the browser starts, the file gets edited with the correct default_directory
path.
Now I have a new use case where I would like to use a new profile. In chrome you can switch between profiles on the browser. I was confused at first because the user-data-dir
argument was already used, but later I realized each "user data directory" can contain one or more profiles. This profile can be set using
options.addArguments("profile-directory=PROFILE 2");
With this additional argument, selenium now loads with a new profile called PROFILE 2 (instead of Default), which is what I want. If the profile doesn't exist, it will be created automatically.
However, one issue I've noticed is the download directory isn't set properly for the new profile; it defaults to windows download folder instead. I decided to look at the Preferences file for the Default profile, and noticed that it was getting the changes instead.
My conclusion here is
- The preferences are being set first
- The profile is then changed
Which is a bit weird to me. I'm not using the latest version of webdriver but I wanted to know if anyone has encountered this issue before, and whether this behavior (setting preferences after changing profile) works properly in later versions.
I thought maybe the order that I set the options might make a difference, but everything goes into the ChromeOptions object and is passed into ChromeDriver during instantiation so I'm not sure if order is something I have control over.