How to open the Default Chrome Profile through Selenium, ChromeDriver and GoogleChrome

只谈情不闲聊 提交于 2020-06-24 05:53:59

问题


I want to load a new Selenium ChromeDriver that is using Chrome as if I open Chrome from my dock (Essentially it'll have all my extensions, history, etc.)

When I use the following code:

ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data\\");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);

It loads the Chrome browser with me signed into my Gmail and with all my extensions, just like I want, but the rest of my code:

chromeDriver.Navigate().GoToUrl("https://www.youtube.com/");

doesn't execute. But when I use the following

ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\Andrea\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);

The rest of my code executes perfectly (Notice the 'Default' added to the end of the first Argument). Any tips or suggestions on how I can get the first block of code (The one without 'Default' on the end) to execute the rest of my program would be great. Thanks!


回答1:


I know this is an old question, but what worked for me is to do remove the "C:\" and replace all of the backslashes with forward slashes. So, with that from the original question, this should work to load the default profile:

options.AddArgument("user-data-dir=/Users/User/AppData/Local/Google/Chrome/User Data");




回答2:


I have the same issue. I don't know how to fix it, I guess the root cause is white space in profile path.

I know a workaround for this. Just copy the C:\\Users\\Andrea\\AppData\\Local\\Google\\Chrome\\User Data to c:\myUserData (no space in the path).

Then add the argument.

options.AddArgument("user-data-dir=C:\\myUserData");



回答3:


The Default Chrome Profile which you use for your regular tasks may contain either/all of the following items:

  • History
  • Bookmarks
  • Cookies
  • Extensions
  • Themes
  • Customized Fonts

All these configurations untill and unless are part of your Test Specification it would be a overkill to load them into the session initiated by Selenium WebDriver. Hence it will be a better approach if you create a dedicated New Chrome Profile for your tests and configure it with all the required configuration.

Here you will find a detailed discussion on How to create and open a Chrome Profile

Once you have created the dedicated New Chrome Profile for your tests you can easily invoke the Chrome Profile as follows:

ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);
chromeDriver.Navigate().GoToUrl("https://www.youtube.com/");

Here you will find a detailed discussion on How to open URL through default Chrome profile using Python Selenium Webdriver



来源:https://stackoverflow.com/questions/51277041/how-to-open-the-default-chrome-profile-through-selenium-chromedriver-and-google

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