How to reuse existing user data directory in Selenium ChromeDriver? - c#

I keep losing browser's session data when trying to recreate new ChromeDriver instance. I was able to logged-in into the GitHub website before calls driver.Quit().
Surprisingly, recreating new driver instance with the same user-data-dir option makes me no longer logged in.
Chrome options
var options = new ChromeOptions();
options.AddArgument($"--user-data-dir={dir}"); // D:\repos\selenium-playground\bin\debug\browser\data
options.AddArgument("--disable-extensions");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-notifications");
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("--no-sandbox");
options.AddArgument("--disable-dev-shm-usage");
options.AddUserProfilePreference("credentials_enable_service", false);
options.AddUserProfilePreference("profile.password_manager_enabled", false);
options.AddExcludedArgument("enable-automation");
options.AddAdditionalCapability("useAutomationExtension", false);
Chrome driver service
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
Creates ChromeDriver instance
I use the same options & service when make authentication to the GitHub website.
var driver = ChromeDriver(service, options);
driver.Navigate().GoToUrl("https://github.com");
Any help will be greatly appreciated!

I would try save cookies after first login, then reusing them on second try. And I don't think, that cookies are saved in this directory.

Related

Chrome Driver Open New Profile With Selenium

I am trying to open chrommium wiht a differenr profile
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddArgument(#"user-data-directory=C:\Users\User\AppData\Local\Google\Chrome\User Data");
options.AddArgument(#"profile-directory=Default - Copy");
driver1 = new ChromeDriver(#"C:\Development\ChromeDrivers\96", options);
This is my user data directory which also has profile directly highlighted. (I just copied the Default folder)
My normal chrome browser has my profile logged in etc
but the chrome browser opened with code is different
What am I missing?

How to make selenium open https page, which displayed manually and not displayed while automated run?

Browsing with chrome to web site with htpps connection manually is available and display the site.
Browsing same site by selenium automated test, doesn't display the page, display empty page.
If i load chrome user profile, automation do open the required page.
Other https site (QA) does displayed through automated connection, the problem is with production sites.
Is there any setting can be added to selenium to open the page?
try this if works
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--start-maximized");
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("disable-infobars");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,
UnexpectedAlertBehaviour.ACCEPT);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver.set(new ChromeDriver(cap));
After the issue solved, posting here the solution.
The site wasn't displayed because automation requests were blocked by company WAF (firewall).
The solution on the automation side was to add agent into chrome profile options and on the waf side rule was added for the agent that allows access to the application.
This is example of how the agent added into ChromeOptions class:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-agent=automation-client");

Selenium chrome driver to open website c# with headless

This is my code with open web.whatsapp.com url and I add argument "Headless" so not open page but give the selection of the which browser to open.
So how to avoid browser selection and get page.
but without "Headless" then easy to open webpage and not asking any option to selection
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
var options = new ChromeOptions();
options.AddArgument("headless");
Driver = new ChromeDriver(service, options);
Driver.Navigate().GoToUrl("https://web.whatsapp.com/");
What worked for me was adding a double-dash before the argument.
chromeOptions.AddArgument("--headless");

Selenium: "DevTools Request: 127.0.0.1:12583/json/version failed" upon instantiation

When I create a new chrome driver in Selenium while Google Chrome is already running AND I am referencing the users settings/data (via user-data-dir). A new Chrome window will open, but my application will hang. The ChromeDriver console will display the following error each second: DevTools Request: 127.0.0.1:12585/json/version. DevTools request failed
Screenshot:
Code to instantiate the driver:
ChromeDriverService driverService = ChromeDriverService.CreateDefaultService();
//driverService.HideCommandPromptWindow = true;
driverService.EnableVerboseLogging = true;
string path = Environment.ExpandEnvironmentVariables("%LOCALAPPDATA%\\Google\\Chrome\\User Data");
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=" + path);
options.AddArguments("--start-maximized");
options.AddArguments("--disable-extensions");
IWebDriver driver = new ChromeDriver(driverService, options);
This will work perfectly fine in every instance if I do not try and load user settings/data. If I am trying to load user setting/data it will only work if there is no instance of Chrome running on the device already.
Versions:
Selenium v 2.47.0
ChromeDriver v 2.16.333243
Chrome v44.0.2403
What can I do to resolve this?
If anyone was looking for answer like me;
https://bugs.chromium.org/p/chromedriver/issues/detail?id=2443
This behavior is by design. Two instances of Chrome can't use the same user-data-dir at the same time. Otherwise they would try to update the same set of files and cause corruptions. So if an instance is already running, attempting to start another instance using the same user-data-dir will cause the second instance to ask the first instance to open a new window, and then the second instance exits.

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

Categories