Load Chrome Profile using Selenium WebDriver using java
Asked Answered
M

8

26

I'm having some troubles getting Selenium loading a chrome profile.

String pathToChrome = "driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", pathToChrome);

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeProfile = "C:\\Users\\Tiuz\\AppData\\Local\\Google\\Chrome\\User Data\\Default";
ArrayList<String> switches = new ArrayList<String>();
switches.add("--user-data-dir=" + chromeProfile);
capabilities.setCapability("chrome.switches", switches);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.google.com");

It starts great and works perfect, but I want to get the default profile loaded because I want to test it with some Extensions enabled and some preferences tested.

Does anybody has an idea why this code isn't working?

I've tested it with Selenium 2.29.1 and 2.28.0 with chromedriver 26.0.1383.0 on windows 7 x64.

Muslin answered 23/1, 2013 at 13:20 Comment(1)
Hi, could you direct me to a tutorial, on how to do this - how to add chrome profiles your your driver? many thanks!Vidette
L
51

This is an old question, but I was still having a problem getting it to work so I did some more research to understand what was happening. The answer from @PrashanthSams is correct, but I was incorrectly adding \Default to the end of the profile path

I found that Chrome appends \Default to the profile path specified in the user-data-dir. So if your profile path is specified as:

user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default\

it will append \Default and you will end up at:

C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default\Default

which is not the same as the profile that you would get if you opened chrome under that user profile normally.

You can also verify your settings if you open a command prompt, navigate to the chrome executable directory, and run chrome with the options specified similar to this:

chrome.exe --user-data-dir="C:\Users\user_name\AppData\Local\Google\Chrome\User Data"

Finally, you can go to a new tab in Chrome and browse to chrome://version/ you will see the actual profile that is being used. It will be listed like:

Profile Path C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default

Leif answered 25/1, 2016 at 19:11 Comment(3)
This was gold. Thanks bro.Tutuila
To select a different profile, say Profile 1, use options.addArguments("profile-directory=Profile 1");Demarcation
Hi, could you direct me to a tutorial, on how to do this - how to add chrome profiles your your driver? many thanks!Vidette
S
21

These combinations did trick for me :)

System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
Saladin answered 7/3, 2013 at 11:46 Comment(2)
Doesn't work for me, it continues to start with a new clean sessionRibwort
This solution didn't work for me either. ` options = webdriver.EdgeOptions() options.add_argument("user-data-dir=C:\\Users\\user_name\\AppData\\Local\\Microsoft\\Edge\\User Data") driver = webdriver.Edge(options=options) driver.maximize_window()`Kure
B
9

You should try this

op.addArgument("--user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data/");

op.addArgument("--profile-directory=Profile 2");
Barrens answered 4/12, 2019 at 16:6 Comment(0)
V
7

Path where Chrome stores the profiles on Linux.

String chromeProfilePath = "/home/(user)/.config/google-chrome/Profile3/";

Creating ChromeOptions object, desabling the extensions and adding the profile I want to use by ".addArguments".

ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("chrome.switches", "--disable-extensions");
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);

As said above by JasonG, after this point Google-Chrome will append \Default to the String you've provided.

There is a "/Default" folder inside "/Profile3" directory, so what I did was...

I copied the content of "/Profile3" to the "/Default" folder.

Set the Browser System Properties and Path as you usually do, call the constructor that receives a ChromeOption and it will work fine.

WebDriver driver = new ChromeDriver(chromeProfile);
Vuillard answered 27/3, 2017 at 21:43 Comment(2)
This one is working for me! Thanks, Thiago and JasonG! I tried renaming my existing profile "Profile 6" to "selenium" and copied the contents of "Profile 6" under "selenium\Default" on Windows 10 and it worked.Urus
On Mac I've used String chromeProfilePath = "/Users/<User Name>/Library/Application Support/Google/Chrome";Memorialize
F
3

I copied default profile to any other folder and then I do connect to this copy

ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-data-dir=C:\\AnyFolder");
driver = new ChromeDriver(options);

So it uses default profile

Fatality answered 15/8, 2013 at 13:30 Comment(0)
P
1

I tried in windows and following code works for me:

String userProfile= "C:\\Users\\user_name\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");

How to know whether it is working ?
One way to know is to run the program twice without killing previous instance of the chrome. If the profile is valid, you'll see the second instance "as a new tab" in the first browser window. If it is not working, you get the second instance "as a new browser window".

Pisciform answered 19/9, 2013 at 14:15 Comment(0)
A
0

According to the ChromeDriver wiki, this is a known issue and is currently not possible.

https://code.google.com/p/selenium/wiki/ChromeDriver

Anthesis answered 5/3, 2013 at 20:41 Comment(0)
H
0

None of these above solutions works for me. And after several hours tirelessly research and trying, I already found out this solution

        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--start-maximized");
        //chrome://version/
        
        chromeOptions.addArguments("--user-data-dir=/home/{username}/.config/google-chrome");
        //load default profile
        chromeOptions.addArguments("--profile-directory=Default");
Hartzog answered 12/10, 2021 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.