使用Java使用Selenium WebDriver加载Chrome配置文件 [英] Load Chrome Profile using Selenium WebDriver using java

查看:612
本文介绍了使用Java使用Selenium WebDriver加载Chrome配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 字符串pathToChrome =driver / chromedriver.exe ; 
System.setProperty(webdriver.chrome.driver,pathToChrome);

DesiredCapabilities功能= DesiredCapabilities.chrome();
字符串chromeProfile =C:\\ Users \\Tiuz\\AppData\\Local\\Google\\Chrome\\User Data\\默认;
ArrayList< String> switches = new ArrayList< String>();
switches.add( - user-data-dir =+ chromeProfile);
capabilities.setCapability(chrome.switches,开关);
WebDriver驱动程序=新的C​​hromeDriver(功能);
driver.get(http://www.google.com);

它开始很好,工作完美,但我想要加载默认配置文件,因为我想测试它启用了一些扩展功能,并测试了一些首选项。



有人知道为什么这段代码无法工作吗?

我已经在Windows 7 x64上使用Selenium 2.29.1和2.28.0与chromedriver 26.0.1383.0进行了测试。 解决方案

<这是一个古老的问题,但我仍然有一个问题让它工作,所以我做了一些更多的研究来了解发生了什么。 @PrashanthSams的回答是正确的,但我错误地将 \默认添加到配置文件路径的末尾



我发现Chrome会在 user-data-dir 中指定的配置文件路径附加 \ Default 。因此,如果您的个人资料路径指定为:



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



它会追加 \默认,最终结果为:



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



这与您获得的个人资料不一样通常在该用户配置文件下打开chrome。



如果您打开命令提示符,导航至chrome可执行文件目录并使用指定的选项运行chrome,则也可以验证您的设置:



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



最后,您可以转到Chrome中的新选项卡并浏览至 chrome://版本/ 您将看到正在使用的实际配置文件。它将列出如下:



个人资料路径 C:\ Users \ user_name\AppData\Local\Google\Chrome \用户数据\默认


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.

解决方案

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

这篇关于使用Java使用Selenium WebDriver加载Chrome配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆