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

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!

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");

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

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");

This is an Old question, but if you are facing this issue, all you have to do is close all tabs, Just shut down the chrome window..
Selenium can't use the data since it is already in use.
Hope you fond this helpful.

Related

Selenium ChromeDriver doesn't Navigate with --headless in console application c#

Good day to all! I have a problem using Selenium ChromeDriver in console application, i use code like this:
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--no-sandbox");
options.AddArgument(#"user-data-dir=G:\Data\ProfilesData\Profile1");
var driverService = ChromeDriverService.CreateDefaultService(Directory.GetCurrentDirectory());
driverService.HideCommandPromptWindow = true;
WebDriver driver = new ChromeDriver(driverService, options);
driver.Navigate().GoToUrl(URL);
Please help me understand so that the browser goes to the desired URL in the console application
I tried to remove the --headless parameter, the browser starts and goes to the desired page, this process does not work without visualization
I found a problem in using the profile, when commenting out the line with connecting the profile everything works, I found a comment on one of the sites that says about a Chrome bug, it cannot work with a profile in headless mode. But everything works fine on my local machine, it doesn't work on the server machine. It is necessary to look for some settings of the server machine.

How to identify chromedriver.exe?

I have some RPA processes which use ChromeDriver to connect to specific instances of Chrome identified by its remote-debugging-port and a custom command arument for chrome which idenitifies the specific chrome instances as "from my program".
I have a program using Selenium ChromeDriver. I do not let ChromeDriver create the Chrome Instance, but rahter re-use an existing one. So when my program starts up it can find the specific Chrome I want to use, and connect a new ChromeDriver to it. The issue is that sometimes the ChromeDriver is left running stuck because I exited without Quitting it (this is development work - stuff happens!)
I would like to be able to somehow tag the ChromeDriver that was launched by my programs. Iti is easy to tage the Chrome instance - I just pass in any unique argument and it happily accepts it, as in "--originator=me", then I can go through the chrome processes, get their command line and know which Chrome is mine. But I do not know how to do this with ChromeDriver. Here are ideas I have that have not worked:
Pass in a command line argument to chromedriver, but the only argument I see in the process is "port=12345"
Get the Process ID of the chromedriver I just created. But I do not know how to ask the new driver for this, even though I have access to it.
Any ideas on how to identify a chromedriver that my process created, assuming that later on my program will be looking for this specific instance?
You can use a ChromeDriverService to create a ChromeDriver then give the service a LogPath. That LogPathcan be the identifier as long as it's a valid file name. Just like this:
var svc = ChromeDriverService.CreateDefaultService();
svc.LogPath = LogPath
ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver(svc, options);
Then if you want to find the specific ChromeDriver, just find the windows process with this command-line argument. In this case we really don't care about the contents of that file itself, just the fact that the magic word is now programmatically discoverable.

Selenium Firefox Preference Changed but not Applied

I am using Selenium and Firefox for automated testing, and I need the files to download automatically. Here are two links that I've used to setup my code.
Auto download PDF in Firefox
Set Firefox profile to download files automatically using Selenium and Java
To summarize the articles, the code should look like this:
FirefoxOptions options = new FirefoxOptions();
options.setPreference("browser.download.folderList", 2);
options.setPreference("browser.download.dir", "C:\\Windows\\temp");
options.setPreference("browser.download.useDownloadDir", true);
options.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer
WebDriver driver = new FirefoxDriver(options);
When I run my test, the auto-download fails. I checked in the about:config and the settings have been changed as intended by the code.
(about:config screenshot)
Also, within that driver instance, if I change any setting and then reapply the same setting, the auto-download works. Is there a setting or step with the webdriver that I'm missing that then applies the new settings?
Here are the Selenium, Firefox, and GeckoDriver versions I've tested with:
Selenium: v3.12.0
Firefox: 59.0.3, 60.0.1
GeckoDriver: v0.19.0-win64, v0.20.0-win64, v0.21.0-win64
As far as i know is pretty difficult download files with selenium because the browser open some dialogs that is not possible control from javascript. Watch this link, I hope will be useful

Disable security features in Firefox

I am testing a website using selenium web driver. The particular site contains nested iframes. I cannot access content of Iframes, because of the Same Origin Policy. Therefore I disabled web security in chrome web driver and access those contents using following Jquery script.
$(‘#data’).find(‘iframe’).contents().find(‘html’)
I achieved this in chrome by setting these features when it is initializing.
ChromeOptions options = new ChromeOptions();
options.AddArguments("--allow-file-access-from-files");
options.AddArguments("--disable-web-security");
IWebDriver driver = new ChromeDriver(options);
(Domain of iframes are differ with my hosted domain.)
Now I need to do this in Firefox. I followed instructions in this URL. But that is not working.
about:config -> security.fileuri.strict_origin_policy -> false
Then I tried with these edits
about:config -> security.mixed_content.block_active_content -> false
about:config -> security.mixed_content.block_display_content -> false
Then I loaded Firefox profile and init web driver using that.
FirefoxProfile profile = new FirefoxProfile(#"C:\Users\admin\AppData\Roaming\Mozilla\Firefox\Profiles\0nan0gbv.default");
IWebDriver driver = new OpenQA.Selenium.Firefox.FirefoxDriver(profile);
I haven't got what I expected using above edits. Error was Permission denied to access property 'document'
Then I tried set these settings into new Firefox profile(not use default one in my machine) and load it.
OpenQA.Selenium.Firefox.FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.privatebrowsing.autostart", true);
profile.SetPreference("security.fileuri.strict_origin_policy", false);
profile.SetPreference("security.mixed_content.block_active_content", false);
profile.SetPreference("security.mixed_content.block_display_content", true);
IWebDriver driver = new OpenQA.Selenium.Firefox.FirefoxDriver(profile);
Then I got this error and couldn't able to find proper solution.
Preference security.fileuri.strict_origin_policy may not be overridden: frozen value=False, requested value=False
I found this question has many ways to overcome SOP. But those are not answers for my problem.
There should be way to achieve this. I am using Firefox 29.0.1

How to Detach Chrome Browser from Chrome Driver (Selenium Web Driver C#)

I want to use Selenium Web Driver in VS 2010 C# to open a Chrome browser, navigate to some web page and then close the driver but keep the browser open. I realize that I will have to manually close the browser afterwards and I'm okay with that.
So far I have:
DriverService service = ChromeDriverService.CreateDefaultService();
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("chrome.detach",true);
m_driver = new ChromeDriver(service, options, TimeSpan.FromMilliseconds(1000));
[m_driver does stuff like navigate page, double click stuff, etc]
[last line: try to close driver but not browser]
I have tried all the following as the last line
m_driver.Dispose(); // closes both browser and driver
m_driver.Close(); //closes just the browser and not the driver
m_driver.Quit(); // closes both browser and driver
service.Dispose(); // closes both browser and driver
Any ideas?
We can detach chrome instance from chromedriver using "detach" options.
Sample Code:
ChromeDriverService cdservice = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("/path/to/chromedriver.exe"))
.withLogFile(new File("/path/to/chromedriver.log"))
.usingAnyFreePort().withVerbose(true).build();
cdservice.start();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("detach", true);
ChromeDriver driver = new ChromeDriver(cdservice,options);
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
driver.get("http://www.google.com/");
// Do not call driver.quit().. instead stop chromedriver service.
cdservice.stop();
This is simply not possible, that kind of separation does not exist.
chromeservice.driver.close()
has worked for me in the past but in this case you may need to write some coding for a method.
It is posible a least in c# you need the next lines:
driverOptions.AddExcludedArgument("enable-automation");
driverOptions.AddAdditionalCapability("useAutomationExtension", false);
(Python) When I called the selenium .get() function, Chrome would open up and close shortly after. I found online the advice to use the following code to remedy this issue: It worked for me in Python3; MacOSX 12.3 Monterey; Chrome Version 105.0.5195.102
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(chrome_options=options, executable_path='/Users/<user_acct_name>/.wdm/drivers/chromedriver/mac64/105.0.5195/chromedriver')

Categories