[英]Load Chrome Profile using Selenium WebDriver using java
I'm having some troubles getting Selenium loading a chrome profile.我在让 Selenium 加载 chrome 配置文件时遇到了一些麻烦。
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.我已经在 Windows 7 x64 上使用 Selenium 2.29.1 和 2.28.0 以及 chromedriver 26.0.1383.0 对其进行了测试。
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 @PrashanthSams 的答案是正确的,但我错误地将\\Default
添加到配置文件路径的末尾
I found that Chrome appends \\Default
to the profile path specified in the user-data-dir
.我发现 Chrome 将\\Default
附加到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:它将附加\\Default
并且您最终会在:
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.这与您在该用户配置文件下正常打开 chrome 时获得的配置文件不同。
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 可执行目录,并使用与此类似的指定选项运行 chrome,您还可以验证您的设置:
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.最后,您可以转到 Chrome 中的一个新选项卡并浏览到chrome://version/
您将看到正在使用的实际配置文件。 It will be listed like:它将如下列出:
Profile Path C:\\Users\\user_name\\AppData\\Local\\Google\\Chrome\\User Data\\Default配置文件路径C:\\Users\\user_name\\AppData\\Local\\Google\\Chrome\\User Data\\Default
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);
Path where Chrome stores the profiles on Linux. Chrome 在 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 对象,禁用扩展并添加我想通过“.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.正如上面 JasonG 所说,在此之后,Google-Chrome 会将 \\Default 附加到您提供的字符串。
There is a "/Default" folder inside "/Profile3" directory, so what I did was... “/Profile3”目录中有一个“/Default”文件夹,所以我所做的是......
I copied the content of "/Profile3" to the "/Default" folder.我将“/Profile3”的内容复制到“/Default”文件夹。
Set the Browser System Properties and Path as you usually do, call the constructor that receives a ChromeOption and it will work fine.像往常一样设置浏览器系统属性和路径,调用接收 ChromeOption 的构造函数,它会正常工作。
WebDriver driver = new ChromeDriver(chromeProfile);
You should try this你应该试试这个
op.addArgument("--user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data/");
op.addArgument("--profile-directory=Profile 2");
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所以它使用默认配置文件
I tried in windows and following code works for me:我在 Windows 中尝试过,以下代码对我有用:
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.一种知道的方法是在不杀死之前的 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".如果它不起作用,您将获得第二个实例“作为新的浏览器窗口”。
According to the ChromeDriver wiki, this is a known issue and is currently not possible.根据 ChromeDriver wiki,这是一个已知问题,目前无法实现。
https://code.google.com/p/selenium/wiki/ChromeDriver https://code.google.com/p/selenium/wiki/ChromeDriver
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");
I managed to launch my chrome profile using these arguments:我设法使用这些参数启动了我的 chrome 配置文件:
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("--profile-directory=Profile 2");
WebDriver driver = new ChromeDriver(options);
You can find out more about the web driver here您可以在此处找到有关 Web 驱动程序的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.