0

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

  1. The preferences are being set first
  2. 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.

MxLDevs
  • 19,048
  • 36
  • 123
  • 194
  • doesn't seem like it should be changing any preferences file(s)... I thought those were all temporary changes for the session. Can you include your paths so we can reproduce your results on our end(s)? It's possible the download dir you are trying to set is not allowed and so it falls back to default. – pcalkins Apr 21 '21 at 22:09
  • @pcalkins Yes, by default selenium generates temporary userdata and profile. Using the `user-data-dir` allows you to point to a static location, and the `profile-directory` will use a custom profile inside that userdata dir. Changes made will persist across sessions. I have included the paths that I use for the download dir and the userdata dir. Everything works when I use the "Default" profile inside my static userdata. It's when I try to use a second profile in the folder, then things are weird. – MxLDevs Apr 21 '21 at 22:39
  • I ended up just using separate `user-data-dir` each with one profile, which works for my needs. I don't think there's much difference besides extra disk space required since each profile is quite bulky on its own as well. – MxLDevs Apr 21 '21 at 22:44
  • you can also skip the "profile-directory=PROFILE 2" part... just add that folder in to your "user-data-dir=..." setting. Maybe that would help avoid the issue? – pcalkins Apr 21 '21 at 22:46
  • 1
    @pcalkins that would solve my issue indeed. It creates separate chrome user data folders, each with a "Default" profile. I'm not sure if there are situations where someone might actually want to use different profile directories though. – MxLDevs Apr 22 '21 at 00:36
  • Maybe this will solve your issue. It's written in Python: https://stackoverflow.com/a/61626506/4103672 – gsa Apr 28 '22 at 18:23

0 Answers0