Selenium ChromeDriver C# only works in headless mode - c#

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!

Related

Unable to start chrome process in .NET C# application

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

OpenQA.Selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally while executing tests through Selenium start on linux

I created an application that use Selenium using .NetCore for use it on Linux, actually this is my code implementation:
public class Program
{
public static async Task Main(string[] args)
{
//Settings for chrome
var chromeOpts = new ChromeOptions();
chromeOpts.AddArgument("headless");
chromeOpts.AddArgument("no-sandbox");
//Get assembly path where chrome driver is located
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
path = Path.GetDirectoryName(path);
var driver = new ChromeDriver(path, chromeOpts, TimeSpan.FromSeconds(180));
}
}
as you can see I am using as driver Chrome, I downloaded it here, I also added the driver inside the folder of the assembly, in this way the ChromeDriver knows already where to search it.
On linux I changed the folder permission using chmod -R 777, but when I run my Dotnet application I get this:
Seems that Selenium cannot start the application.
Looking at the Exception I get:
Cannot find Chrome binary
Unfortunately I didn't find anything similar on the web, someone could help me?
Thanks in advance.
UPDATE
I Just reinstalled Chrome on my linux machine and now the error above went away, but there is another problem now, infact I get this error:
OpenQA.Selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.9.248304,platform=Linux 4.4.0-130-generic x86_64)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout)\ at ODS.Program.Main(String[] args)
This error message...
OpenQA.Selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using chromedriver=2.9 which is pretty ancient.
So there is a clear mismatch between the ChromeDriver version (v2.33) and the recent Chrome Browser version (vVersion 68.0)
Solution
Upgrade ChromeDriver to current ChromeDriver v2.41 level.
Keep Chrome version between Chrome v67-69 levels. (as per ChromeDriver v2.41 release notes)
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Execute your #Test.
References
You can find a couple of relevant discussions in:
WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium on debian server
Message: unknown error: Chrome failed to start: exited abnormally on AWS Cloud9 with Linux 4.9.85-38.58.amzn1.x86_64 x86_64
WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium through Python on VPS
first, check your google-chrome version
then download related chromedriver version i.e

Required parameters for launching headless chrome

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

Geckodriver with selenium 3.0 throws DriverServiceNotFoundException

I need help to upgrade to geckodriver using C#. I downloaded geckodriver from here. Downloaded windows 64bit version as I'm on windows 10 64bit. Copied the driver to my project location.
Environment.SetEnvironmentVariable("webdriver.gecko.driver", #"C:\Git\AutomationTest\Drivers\geckodriver.exe");
FirefoxDriverService driverService = FirefoxDriverService.CreateDefaultService();
driverService.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
driverService.HideCommandPromptWindow = true;
driverService.SuppressInitialDiagnosticInformation = true;
driver = new FirefoxDriver(driverService, new FirefoxOptions(), TimeSpan.FromMilliseconds(600));
It threw error:
Initialization method UnitTestProject1.UnitTest1.Init threw exception. OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases..
Result StackTrace:
at OpenQA.Selenium.DriverService.FindDriverServiceExecutable(String executableName, Uri downloadUrl)
at OpenQA.Selenium.Firefox.FirefoxDriverService.CreateDefaultService()
at OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxOptions options)
at OpenQA.Selenium.Firefox.FirefoxDriver..ctor()
at UnitTestProject1.UnitTest1.Init()
Tried renaming it to 'Wires' but didn't work. Searched so many questions on SO, didn't find a solution with Selenium 3.0.
Added the path of the folder that has the driver to System variables path and tried using DesiredCapabilities.
DesiredCapabilities cap = DesiredCapabilities.Firefox();
cap.SetCapability("marionette", true);
var driver = new RemoteWebDriver(cap);
Using Selenium 3.0, FF 47.0.1, gecko v0.11.1
Can someone help me with this issue.
Thanks.
Try this:
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService("C:\Git\AutomationTest\Drivers");
IWebDriver driver = new FirefoxDriver(service);
I updated Firefox to Version 49.0.2 and updated my selenium driver to 3.0.0 from nuget packages. Added Firefox path to the system path variables. That's it I didn't change anything else in my coding i.e, declaration of Firefox. After updating I'm having too many issues like wait and System.Net.Web exception. I need to fix some of my test cases but it works.

c# Selenium 2.53 moving to marionette driver after firefox upgrade to 47

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.

Categories