26

I have created a player which will automate chrome using selenium and ChromeDriver in C#. It's working fine.

Issue what I am facing is, when it creates an object for ChromDriver, it will start ChromeDriver application, which gets pop up and then Chrome will load. It's perfect as that application is loading that chrome for me.

Is there anyway, that I can open that ChromeDriver hidden?

4
  • This software is meant for testing. And in that regard, a console window is a welcome thing. I'm not sure what you're using this for, but it sounds like its not testing. There may be a better solution to your underlying problem, like making API call directly instead of going through a browser. Commented Jun 23, 2017 at 15:18
  • That console window is only of annoyance. Commented Aug 20, 2018 at 7:20
  • 3
    @DanCsharpster "Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) be automated as well." - seleniumhq.org
    – Tyler
    Commented Sep 17, 2018 at 18:22
  • 1
    Quite true, @Samir. I was just trying suggest an alternative as working with browsers can be clunky and less reliable than using an API. That said, since I posted this comment, I have started using headless Chrome with Selenium and absolutely love it! Sanket Shah, you should check it out! developers.google.com/web/updates/2017/04/headless-chrome Commented Sep 18, 2018 at 0:54

11 Answers 11

68

Modifying source code in WebDriver\DriverService.cs is not necessary for this in latest WebDriver. You just need to instantiate ChromeDriverService and set HideCommandPromptWindow to true and then instantiate ChromeDriver by that service and ChromeOptions. I am giving C# code example below

var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
return new ChromeDriver(chromeDriverService,  new ChromeOptions());
2
  • 3
    You can also add the path to the driver binary ChromeDriverService::CreareDefaultService( pathToBinary );
    – kirsche40
    Commented Jun 18, 2015 at 15:28
  • Using the "DriverService" concept here worked for Chrome, Firefox, and IE (didn't try others... you just instantiate the proper service from the corresponding namespace).
    – HgCoder
    Commented Nov 7, 2018 at 16:48
9

No, there is no way to hide the console window of the chromedriver.exe in the .NET bindings without modifying the bindings source code. This is seen as a feature of the bindings, as it makes it very easy to see when your code hasn't correctly cleaned up the resources of the ChromeDriver, since the console window remains open. In the case of some other languages, if your code does not properly clean up the instance of ChromeDriver by calling the quit() method on the WebDriver object, you can end up with a zombie chromedriver.exe process running on your machine.

4
  • Hello @JimEvans - I'm actually having a reverse problem on one of my computers. This computer hides all console windows of IEDriverServer and ChromeDriver and I don't know why. The code doesn't have any of these specific options and the windows are visible when the code is run on other computers. Any insight? Thanks!
    – AngieM
    Commented Oct 17, 2016 at 13:19
  • @AngieM Are you using the .NET language bindings? If not, then the information in this question does not apply to you. Other languages (Python, Ruby, Java) may not give you any control at all over whether a separate command prompt window appears.
    – JimEvans
    Commented Oct 17, 2016 at 13:25
  • Yes I'm using C#. I do not want to hide the console windows. They're just hidden on this one particular computer for some reason (Windows 8.1 and VS2015).
    – AngieM
    Commented Oct 17, 2016 at 13:40
  • Can you see the processes? Is it running as a different user than the currently logged on one? Commented Jun 23, 2017 at 15:19
6

Yes, you need modify source code in WebDriver\DriverService.cs in Start(); add:

this.driverServiceProcess.StartInfo.CreateNoWindow = true;
1
  • 1
    Beware of doing this without using the full build process for the Selenium project. Simply building in Visual Studio will not give you an equivalent assembly to that generated by the Selenium project's build script.
    – JimEvans
    Commented May 1, 2013 at 22:40
3

This is possible now, using the headless option. Might not have been available then. Running Chrome Headless

Translated into C#, you can do this:

ChromeOptions options = new ChromeOptions();
options.AddArgument("headless");
ChromeDriver driver = new ChromeDriver(options);

Now the browser window is invisible, while the rest of the program stays the same.

1
  • How to do this in VB .NET?
    – frozenade
    Commented Mar 19, 2019 at 2:41
3

As an update to this question. Yes, you can hide command prompt window in Selenium 2.40.0 and up. Please note that hiding command prompt window is not recommended but you can do it. As the question refers to C# you do it with the next code:

ChromeDriver

var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new ChromeDriver(driverService, new ChromeOptions());

InternetExplorerDriver

var driverService = InternetExplorerDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new InternetExplorerDriver(driverService, new InternetExplorerOptions());

PhantomJSDriver

var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new PhantomJSDriver(driverService);
0
drv.Manage().Window.Position = new Point(3000, 3000);

will fix your problem.

1
  • This doesn't fix the problem, it just sweeps it under the carpet... Commented Aug 5, 2021 at 10:12
0

I did this using c#, but like JimEvan's repply, I had some problems with "zombies chromedrivers and cmd zombies". After doing "headless mode" and "hiding" the prompt window.

Don't forget to close your chrome driver instance correcly.

To solve this, doing a little google how kill some process, I kill these two process, before open another chrome driver again :

static void Main(string[] args)
{
    ChromeDriverService _chromeDriverService;
    ChromeOptions _chromeOptions;
    IWebDriver _driver;

    //set ChromeDriverService, to hide prompt widow
    _chromeDriverService = ChromeDriverService.CreateDefaultService("YOUR CHROME DRIVER PATH HERE");
    _chromeDriverService.HideCommandPromptWindow = true;

    //set ChromeOptions, to hide ChromeDriver 
    _chromeOptions = new ChromeOptions();
    _chromeOptions.AddArguments("headless");

    _driver = ChromeDriver(_chromeDriverService, _chromeOptions);

    //do your things...
}

//call this method before run your chrome driver, to close some ghost chromeDriver or prompt window
private static void CloseGhostsChromeDriver()
{
    Process[] cmd = Process.GetProcessesByName("cmd");
    Process[] chromeDriver = Process.GetProcessesByName("chromedriver");

    Process[] workers = chromeDriver.Concat(cmd).ToArray();

    foreach (Process worker in workers)
    {
        worker.Kill();
        worker.WaitForExit();
        worker.Dispose();
    }
}
0

In updated version of ChromeDriver you don't need to edit the source code just with:

var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
0

If you want to hide the console that is opened when launching chrome, firefox, etc.. you will need a helper class like this:

static class WindowsUtils { [DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

// Delegate to filter which windows to include 
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

/// <summary> Get the text for the window pointed to by hWnd </summary>
public static string GetWindowText(IntPtr hWnd)
{
    int size = GetWindowTextLength(hWnd);
    if (size > 0)
    {
        var builder = new StringBuilder(size + 1);
        GetWindowText(hWnd, builder, builder.Capacity);
        return builder.ToString();
    }

    return String.Empty;
}

/// <summary> Find all windows that match the given filter </summary>
/// <param name="filter"> A delegate that returns true for windows
///    that should be returned and false for windows that should
///    not be returned </param>
public static IEnumerable<IntPtr> FindWindows(EnumWindowsProc filter)
{
    IntPtr found = IntPtr.Zero;
    List<IntPtr> windows = new List<IntPtr>();

    EnumWindows(delegate (IntPtr wnd, IntPtr param)
    {
        if (filter(wnd, param))
        {
            // only add the windows that pass the filter
            windows.Add(wnd);
        }

        // but return true here so that we iterate all windows
        return true;
    }, IntPtr.Zero);

    return windows;
}

/// <summary> Find all windows that contain the given title text </summary>
/// <param name="titleText"> The text that the window title must contain. </param>
public static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
{
    return FindWindows(delegate (IntPtr wnd, IntPtr param)
    {
        return GetWindowText(wnd).Contains(titleText);
    });
}

}

An then you can create your Selenium driver like this (also hidding the Firefox window).. If you want to use Chrome only have to do some minor changes... I prefreer Firefox because Chrome sometimes don't work with headless option:

    FirefoxOptions options = new FirefoxOptions();            
    options.SetPreference("permissions.default.image", 2); //prevent download images
    options.AddArguments(new string[] { "--headless", "'--disable-gpu'" }); //no window, no gpu
    driver = new FirefoxDriver(options);
    WindowsUtils.ShowWindow(WindowsUtils.FindWindowsWithText("geckodriver.exe").FirstOrDefault(), 0); //0 to hide, 1 to show
0

This solution will help you. Please note that hiding command prompt window is not recommended.

ChromeDriver

var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var driver = new ChromeDriver(driverService, new ChromeOptions());
0

I know this isn't what you want but it could help if used in certain situations. Writing driver.quit() will terminate the chrome window and the terminal it has open.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.