I'm using Selenium and C#, headless chrome.
I'm new to C#, so this might be something obvious, but I've looked at other questions and seen to add:
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
I added that to my Start() and the window still pops up, here is my start method:
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
var option = new ChromeOptions();
option.AddArguments("--headless", "--no-sandbox", "--disable-web-security", "--disable-gpu", "--incognito", "--proxy-bypass-list=*", "--proxy-server='direct://'", "--log-level=3", "--hide-scrollbars");
driver = new ChromeDriver(option);
Please let me know if you need anything else, thank you in advance!
You are very nearly at the solution you want. You set the property on the service, but never used it anywhere. What you want is the following:
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
var option = new ChromeOptions();
option.AddArguments("--headless", "--no-sandbox", "--disable-web-security", "--disable-gpu", "--incognito", "--proxy-bypass-list=*", "--proxy-server='direct://'", "--log-level=3", "--hide-scrollbars");
driver = new ChromeDriver(chromeDriverService, options);
Related
With the new Selenium.WebDriver.ChromeDriver.74.0.3729.6 update, the problem came out.
https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=tr
I'm trying to use the extension above.
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddExtensions(#Application.StartupPath.ToString() + #"\block-image.crx");
driver = new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromMinutes(10));
run the program. My result page remains "data :,". does not go to the page I want. different extensions have tried the same unfortunately.
i am trying to change chrome default homepage (google tabs) but i didn' t find a working solution.
What i have tried:
var _options = new ChromeOptions();
_options.AddUserProfilePreference("homepage", "http://www.example.com");
_options.AddUserProfilePreference("homepage_is_newtabpage", true);
_options.AddUserProfilePreference("session.restore_on_startup", 4);
_options.AddUserProfilePreference("session.startup_urls", new List<string>() { "http://in.gr"});
_options.AddArgument("--homepage=http://in.gr");
var _driver = new ChromeDriver(_options);
You can navigate to the page you want to after initializing the WebDriver before you begin the rest of your web automation.
...
var _driver = new ChromeDriver(_options);
_driver.Navigate().GoToUrl("http://in.gr");
Hope that is of some help.
I want to try out headless chrome, but I am running into this issue, that I can't start the driver in headless mode. I was following google documentation. am I missing something ? The code execution gets stuck in var browser = new ChromeDriver(); line
Here is my code:
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\2-as Aukstas\Documents\Visual Studio 2017\Projects\ChromeTest\ChromeTest\bin\Debug\chromedriver.exe",
DebuggerAddress = "localhost:9222"
};
chromeOptions.AddArguments(new List<string>() {"headless", "disable-gpu" });
var browser = new ChromeDriver(chromeOptions);
browser.Navigate().GoToUrl("https://stackoverflow.com/");
Console.WriteLine(browser.FindElement(By.CssSelector("#h-top-questions")).Text);
UPDATE
Chrome version 60 is out so all you need to do is to download Chromdriver and Selenium via Nuget and use this simple code and everything works like a charm. Amazing.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
...
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
using (var browser = new ChromeDriver(chromeOptions))
{
// add your code here
}
DATED
There is a solution until the official release of Chrome 60 will be released. You can download Chrome Canary and use headless with it. After installation set BinaryLocation to point to chrome canary also comment out the DebuggerAddress line(it forces chrome to timeout):
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\2-as Aukstas\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
//DebuggerAddress = "127.0.0.1:9222"
};
chromeOptions.AddArguments(new List<string>() { "no-sandbox", "headless", "disable-gpu" });
var _driver = new ChromeDriver(chromeOptions);
For you that did not get reference for ChromeDriver.
Use this step :
Download the dll from this: http://seleniumtestings.com/selenium-download/
Extract, and you should see: Selenium.WebDriverBackedSelenium.dll, ThoughtWorks.Selenium.Core.dll, WebDriver.dll and WebDriver.Support.dll
Add those files via "Add Reference"
Now you can use it:
String url = "http://www.google.com";
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() {
"--silent-launch",
"--no-startup-window",
"no-sandbox",
"headless",});
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true; // This is to hidden the console.
ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
driver.Navigate().GoToUrl(url);
====
If after you run, you are still facing error about no ChromeDriver.exe file, try to add the Selenium.WebDriver.ChromeDriver, WebDriver.ChromeDriver, WebDriver.ChromeDriver.win32, Selenium.Chrome.WebDriver via nuget.
As alternative:
Add 2 libraries via NuGet like below picture.
Try below Code:
String url = "http://www.google.com";
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() { "headless" });
var chromeDriverService = ChromeDriverService.CreateDefaultService();
ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
driver.Navigate().GoToUrl(url);
What OS you're running? I see on developers.google.com/web/updates/2017/04/headless-chrome that headless won't be available on Windows until Chrome 60.
Below i have given how to set the headless to true for firefox and chrome browsers.
FirefoxOptions ffopt = new FirefoxOptions();
FirefoxOptions option = ffopt.setHeadless(true);
WebDriver driver = new FirefoxDriver(option);
ChromeOptions coptions = new ChromeOptions();
ChromeOptions options = coptions.setHeadless(true);
WebDriver driver = new ChromeDriver(options);
I am trying to run a browser test using xUnit, Selenium, and Chrome Canary (headless mode) but I keep getting this error:
OpenQA.Selenium.WebDriverException
The HTTP request to the remote WebDriver server for URL
http://localhost:58692/session timed out after 60 seconds.
Here's my code:
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\<USERNAME>\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
DebuggerAddress = "127.0.0.1:9222"
};
chromeOptions.AddArguments("no-sandbox", "headless", "disable-gpu");
_driver = new ChromeDriver(chromeOptions) {Url = Url};
I'm quite new to C# so I'm not sure if I'm doing something blatantly wrong, or if I'm just missing a setting. Googling the above error told me that I need to set the debugger address and use the no-sandbox flag, but neither seem to be helping.
Using these versions:
selenium 3.4
chromedriver 2.29
xunit 2.2
Taking out the debugger address made it work.
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\<USERNAME>\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
};
In case that others will get here while googling...
What worked for me was to download the latest chrome-driver from this link:
https://sites.google.com/a/chromium.org/chromedriver/downloads
Happy coding :)
case "chrome9515headless":
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--headless");
chromeOptions.AddArgument("--disable-gpu");
chromeOptions.AddArgument("--disable-infobars");
chromeOptions.AddArgument("--disable-extensions");
chromeOptions.AddArgument("--window-size=1200,900");
chromeOptions.AddArgument("--disable-browser-side-navigation");
webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:9515"),
chromeOptions.ToCapabilities());
break;
case "chrome9515canary":
ChromeOptions chromeOptionsCanary = new ChromeOptions();
chromeOptionsCanary.BinaryLocation= #"C:\Users\********\AppData\Local\Google\Chrome SxS\Application\chrome.exe";
//chromeOptionsCanary.AddArgument("--headless");
//chromeOptions.AddArgument("--disable-gpu");
chromeOptionsCanary.AddArgument("--disable-infobars");
chromeOptionsCanary.AddArgument("--disable-extensions");
chromeOptionsCanary.AddArgument("--window-size=1200,900");
chromeOptionsCanary.AddArgument("--disable-browser-side-navigation");
webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:9515"),
chromeOptionsCanary.ToCapabilities());
break;
This is how I add profile preferences to Chrome for Local Auto-test runs and TeamCity(CI):
Capabilities = DesiredCapabilities.Chrome();
var chromeOptions = new ChromeOptionsWithPrefs();
chromeOptions.AddUserProfilePreference("download.default_directory", DownloadPath);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
return new ChromeDriver(chromeDriverPath, chromeOptions);
But when I create new 'RemoteWebDriver', I must send it a hub URL and 'Capabilities', this way I send profile preferences to Firefox (to RemoteWebDriver):
var profile = new FirefoxProfile();
Capabilities = DesiredCapabilities.Firefox();
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", DownloadPath);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk",
"application/zip, application/octet-stream");
Capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
return Capabilities;
Can someone help me, I need to do the same thing to Chrome as I did to Firefox. Basically what I need, is that I could change the default path for downloading files.
You'll want to do something like the following:
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", DownloadPath);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
IWebDriver driver = new RemoteWebDriver(new Uri("http://path/to/selenium/server"), chromeOptions.ToCapabilities());