I'm trying to play with web forms / loggins, etc. and for this I need to start the browser (chrome, for example) with the current user profile session. I found 2 possible ways of doing this, but I can't finish the code for both of them :)
Solution 1
System.Diagnostics.Process.Start(url);
This will open a new tab in current session with my user profile (perfect!). But what now? I don't have a variable like "browser" to find elements by id, or something like that. Is there anything I can do next to get the control of the page / browser?
Solution 2 (selenium driver)
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=C:/Users/john/AppData/Local/Google/Chrome/User Data/Default");
g.webdriver[0] = new ChromeDriver(options);
a) If I don't use the argument user-data-dir with the path for my user profile, it will open with no errors (default selenium chrome session) and I'll need to log in on my desired web page -> not a good idea for Enterprise stuff. b) Using this code it will open a new chrome browser window with my current session / user profile (perfect!), but the code will fail with the error "DevToolsActivePort file doesn't exist", this means I won't be able to continue and do my stuff. I also tried a lot of things like:
System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"C:\Users\john\.nuget\packages\selenium.chrome.webdriver\2.45.0\driver\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=C:/Users/john/AppData/Local/Google/Chrome/User Data/Default");
options.AddArguments("--no-sandbox");
options.AddArguments("--disable-dev-shm-usage");
options.AddArguments("--disable-gpu");
options.AddArguments("--headless");
options.AddArguments("--no-cache");
options.AddAdditionalCapability("useAutomationExtension", false);
g.webdriver[0] = new ChromeDriver(options);
g.webdriver[0].Manage().Window.Maximize();
g.webdriver[0].Navigate().GoToUrl(url);
I'm back and I FOUND THE SOLUTION! (thanks to this guy)
With the below code you can create NEW PROFILES and reuse them. If you will log in once with the desired accounts (for example your google account), it will remain synchronized! So it's awesome for whatever you need to use it for.
Requirements: Selenium.WebDriver (NuGet) + chromedriver_win32 (version compatible for your chrome version-manually download) + edit in code the "default_profile_dir" and the "chromedriver_path" C# web forms, all code below: