Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

Load Chrome Profile using Selenium WebDriver using java

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:Usersuser_nameAppDataLocalGoogleChromeUser DataDefault

it will append Default and you will end up at:

C:Usersuser_nameAppDataLocalGoogleChromeUser DataDefaultDefault

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:Usersuser_nameAppDataLocalGoogleChromeUser 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:Usersuser_nameAppDataLocalGoogleChromeUser DataDefault


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...