What are the required options for launching google chrome headless from C#? These are the options I’ve got now:
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("start-maximized");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-extensions");
options.AddArgument("--window-size=1024,768");
var driver = new ChromeDriver(options);
It would appear that when running headless chrome from, at least, inside Visual Studio the "no-sandbox" option is required.
options.AddArgument("no-sandbox");
Found by accident here:
https://stackoverflow.com/a/39299877/71376
Related
I am getting error on Selenium and unable to start chrome process. I am using Google Chrome Version 96.0.4664.45 (64-bit) and Selenium Web Driver 3.141.0
ChromeOptions chromeOptions = new ChromeOptions
{
BinaryLocation = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
};
chromeOptions.AddArgument("incognito");
using (ChromeDriver window = new ChromeDriver(chromeOptions))
{
try
{
log.Debug("Chrome window instantiated");
Did you install chromedriver too? If you haven't already: https://chromedriver.chromium.org/downloads
Project Properties:
C# .NET Framework 4.7.2
Repository: https://github.com/theomnislayer/ChromeDriverError
Visual Studio 2019 Pro. The Repo above should work with VS Code
Nuget Packages installed:
Selenium.WebDriver 3.141.0
Selenium.Support 3.141.0
Selenium.WebDriver.ChromeDriver 94.0.4606.6100
Xunit 2.4.1
Xunit.Runner.VisualStudio 2.4.3
Error Description:
Starting from Chrome 93.x and ChromeDriver 93.x or newer
Without the --headless argument in chromeoptions, the following error is encountered:
Message:
OpenQA.Selenium.WebDriverException : unknown error: Chrome failed to start: crashed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Program Files (x86)\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Expected behavior: Chrome should NOT crash even without the headless argument
Replicating the error:
Sample Code:
public void DisabledHeadless()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("--disable-extensions");
options.AddArgument("--safebrowsing-disable-download-protection");
options.AddArguments("no-sandbox");
options.AddArgument("enable-automation");
options.AddArgument("test-type=browser");
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("safebrowsing", "enabled");
options.AddUserProfilePreference("disable-popup-blocking", "true");
//options.AddArgument("--headless");
var driver = new ChromeDriver(
Directory.GetCurrentDirectory(),
options,
TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl("https://www.google.com");
}
Run the test, Error is encountered.
Workaround:
Sample Code:
public void EnabledHeadless()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("--disable-extensions");
options.AddArgument("--safebrowsing-disable-download-protection");
options.AddArguments("no-sandbox");
options.AddArgument("enable-automation");
options.AddArgument("test-type=browser");
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("safebrowsing", "enabled");
options.AddUserProfilePreference("disable-popup-blocking", "true");
//Weird = Starting from V.93, without this argument, CHROME CRASHES!
options.AddArgument("--headless");
var driver = new ChromeDriver(
Directory.GetCurrentDirectory(),
options,
TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl("https://www.google.com");
}
Is there a fix for this without headless enabled? Errors were not encountered with Chrome 92 or prior. Thanks in advance!
I am trying to move into the upgraded firefox web browser automation using selenium. It seems that selenium needs marionette driver to continue working. I followed the instructions set by the devs,
downloaded the driver
renamed it to wires.exe
The following code didnt manage to properly set the PATH to a custom path.
System.Environment.SetEnvironmentVariable("webdriver.gecko.driver", "#C:\DOWNLOADS\wires.exe")
so i added wires.exe to the debug\bin folder and then wires.exe worked properly but i got the following error
System.InvalidOperationException was caught Message=entity not found Source=WebDriver
this is the code i use to start webdriver
FirefoxOptions option1 = new FirefoxOptions();
option1.IsMarionette = true;
option1.AddAdditionalCapability("marionette", true);
driver = new FirefoxDriver(option1);
I too got the "Entity Not Found" error using FirefoxDriver(new FirefoxOptions()). It appears to be looking for firefox.exe in C:\Program Files (x86)\Nightly and not finding it. I found this working :
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
IWebDriver driver = new FirefoxDriver(service);
I try with this and it's working:
Install FirefoxDevEdition
Download geckodriver.exe
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\Users\jmalpartida\Downloads\geckodriver-v0.8.0-win32", "geckodriver.exe");
service.Port = 64444;
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Firefox Developer Edition\firefox.exe";
IWebDriver driver = new FirefoxDriver(service);
First of all, you need to add the driver to your system path, not as an env variable.
Second, you need to set the flag in a desired capability, not a Firefox option. See: Marionette Webdriver
As such for remote webdriver:
DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
capabilities.SetCapability("marionette", true);
var driver = new RemoteWebDriver(capabilities);
To add the webdriver to your windows path:
The easiest way is to open the start menu > search for environment > open edit the system environment variables > click on environment variables > search in the list for Path > click on edit > add ;C:\path\to\webdriver\location\wires.exe to the end and click save.
For your local (non-webdriver) tests you are right, you can run your webdriver using the following:
var driver = new FirefoxDriver(new FirefoxOptions());
You should not have to use
option1.IsMarionette = true;
option1.AddAdditionalCapability("marionette", true);
If you have set the driver path correctly in your path environment variable.
I write automation system and perfectly works in localhost, but when i try to publish it and upload to server. I got this error;
unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64)
And I am 1 billion sure of this path, here is my code
var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
var options = new ChromeOptions();
var driverPath = Path.Combine(outPutDirectory, "ChromeDriverInThisFolder\\");
string driver_path = new Uri(driverPath).LocalPath;
driver = new ChromeDriver(driver_path, options);
My server is windows Windows NT 6.1 SP1 x86_64
2008 R2 Enterprise 64 bit
You can use the "standard location" of chrome, this way:
var options = new ChromeOptions();
// all of your 'options.AddArgument(...);' here
driver = new ChromeDriver(options); //This will look for chrome in the default directory
If you need to pass a chrome binary in a different directory you can use this way:
var options = new ChromeOptions();
// all of your 'options.AddArgument(...);' here
options.setBinary("pathToYourOtherBinary"); //This is for CHROME binary, not ChromeDriver binary
driver = new ChromeDriver(options);
After some research, here and here, I'm tempted to say that you don't have ChromeDriver on your server.
It should be somewhere on your AppData like this:
Windows XP %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
Windows Vista C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe
Donwload ChromeDriver from here and install it on your server. Than the first option (whithout the path) will work.
Is it possible to test browser extensions with selenium c# in Saucelab. If so how and where to place the latest extension package in saucelab VM so that selenium launches the browser with the extension.
For Firefox and chrome it is possible by providing capabilities or profile. For Example in case of firefox:
File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen
WebDriver driver = new FirefoxDriver(firefoxProfile);