Selenium chrome driver to open website c# with headless - c#

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

Related

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

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.

Chrome driver hangs when using user data

The browser opens but the debugger hangs. If I comment out the AddArgument line then it proceeds fine. Any ideas?
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--user-data-dir=C:\\Users\\Ian\\AppData\\Local\\Google\\Chrome\\User Data");
IWebDriver driver = new ChromeDriver("C:\\Users\\Ian\\Desktop", chromeOptions);
It seems to want all other Chrome windows closed to work with existing user data.

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