Chrome Driver Open New Profile With Selenium - c#

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?

Related

C# selenium chrome driver, every time I run my program I need to scan QR code from my mobile

ChromeOptions options = new ChromeOptions();
options.AddArguments("--user-data-dir=C:\\Users\\Myname\\AppData\\Local\\Google\\Chrome\\User Data");
options.AddArguments("--profile-directory=Profile 3");
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://web.whatsapp.com/");
How can I store the whatsApp login session, So that every time I run a program I don't need to scan the QR code again?
When you quit the webdriver, the cookies, cache, history, etc are deleted. So if you want to be already logged in when you start the webdriver, you have to use the cookies from a profile on the normal browser in which you are already logged in. To do so you have to:
create a new chrome profile in the normal browser
login to whatsapp
load the profile in selenium
In this way when you start the webdriver and load the page you will be already logged in.
To create a new profile, open your chrome browser, click on the profile icon on top right, then click "Add" and then click "Continue without an account".
Then write a name for your profile and be sure check the box "Create a desktop shortcut". I suggest you to choose a color so that you will immediately see if it works when selenium open the browser window.
Open chrome from the new desktop icon (mine is called "PythonSelenium - Chrome.exe") and login to instagram.
After you logged in, close the browser and open the properties of the new desktop shortcut. Take note of the name of the profile directory, which in my case is "Profile 3".
Now we have to tell selenium to open the chrome driver using this new profile we've just created, which is logged in twitch. To do this we need the path of the folder where the profile is stored. By default the path is something like C:\Users\your_username\AppData\Local\Google\Chrome\User Data, if you are not sure about it check where chrome is installed in your computer.
Then run this code (remember to substitute Profile 3 with the name of your profile from step 3)
ChromeOptions options = new ChromeOptions();
options.AddArguments("--no-sandbox")
options.AddArguments("--remote-debugging-port=9222")
options.AddArguments("--disable-dev-shm-using")
options.AddArguments("--disable-setuid-sandbox")
options.AddArguments("--user-data-dir=C:\Users\your_username\AppData\Local\Google\Chrome\User Data");
options.AddArguments("--profile-directory=Profile 3"); // <-- substitute Profile 3 with your profile name
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://web.whatsapp.com/");

How to reuse existing user data directory in Selenium ChromeDriver?

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.

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

C# chrome driver only opening as background process on IIS / Windows Server 2016

I am running chromedriver on windows server 2016 with IIS, i have my test project installed and invoking it with an MVC5 API. That all seems fine but chromedriver and chrome.exe only seems to open as a background processes.
The same code opens these fine locally, i am not using any of the driver flags for headless browsing either. if i return the drive page source i can see that chromedriver went to google and returned the correct html in my API.
It just does not work for normal / non headless tests with google or our application.
var driver = x.StartWebDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("http://www.google.com");
return driver.PageSource;
Any ideas?
I have got the same issue and this is how I solve this:
Open a command prompt and navigate to your ChromeDriver location.
Execute chromedriver.exe and you will see the message as below.
image here
In the image above you see chrome driver is listening on port 9515.
Now change your code as below, rebuild and call you api to execute your test.
ChromeOptions options = new ChromeOptions();
//set your chromeoptions here
Uri uri = new Uri("http://localhost:**9515");
_driver = new RemoteWebDriver(uri, options);
Open the chrome driver and note the port number(eg:5353)
In java:
System.setProperty(chromeDriverName, chromeDriverLocation);
ChromeOptions options = new ChromeOptions();
URL uri = new URL(chromeDriverPort);//http://localhost:5353
WebDriver driver = new RemoteWebDriver(uri, options);
your problem gets solved in is server.

Categories