Load Chrome Profile using Selenium WebDriver using java

為{幸葍}努か 提交于 2019-12-16 22:48:09

问题


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.


回答1:


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




回答2:


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



回答3:


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



回答4:


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




回答5:


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".




回答6:


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

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




回答7:


Try this below

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

op.addArguments("--profile-directory=Profile 2");



来源:https://stackoverflow.com/questions/14480717/load-chrome-profile-using-selenium-webdriver-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!