简体   繁体   中英

How to hide the chromedriver.exe console window in C#?

I am writing a two programs using C# and I am using this references, installed throught NuGet in both:

<package id="Selenium.WebDriver" version="3.141.0" targetFramework="net472" />
<package id="Selenium.WebDriver.ChromeDriver" version="79.0.3945.3600" targetFramework="net472" />

The code of one of programs simply opens Google Chrome browser with a Default Profile and a own extension, it navigate to a page and does some operations. The other program searches for the open browser and does an operation. When the both programs run a console window appears (it seems to belong to chromedriver.exe) and then the browser opens. When the programs ends, the console window remain open. This second program will run many times. My problem is that every time the program ends the console window remains open.

It's possible to say that I've trying some things to see how it behave: If I close the browser manually the console window remains open, but if I close the console window manually instead of the browser, the browser closes. This makes it clear that the console window must be open to the browser stay open. I've tried using driver.Quit() and driver.Close() too. The first closes the browser and console window, the second only the browser.

My idea is to run the first program when the computer turns on and run the second program from another computer as many times as necessary. But I don't want a console window to remain open every time it runs. Is there any way that this console window does not open?

My code of the first program is the next:

ChromeOptions options = new ChromeOptions();
    options.AddExtension("[Path]");
    options.AddArgument("--remote-debugging-port= [PortNumber]");
    options.AddArgument("--user-data-dir=[Path]");

IWebDriver driver = new ChromeDriver(options);
    driver.Navigate().GoToUrl("http://www.google.es");

and the second program is this:

ChromeOptions options = new ChromeOptions();
    options.DebuggerAddress = "localhost:[PortNumber]";

IWebDriver driver = new ChromeDriver(options);
    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

IWebElement element;
    element = driver.FindElement(By.ClassName("[ClassnName]"));
    element.Click();

You need to modify source code, try to use like this:

private IWebDriver GetSeleniumDriver()
{
  var chromeDriverService = ChromeDriverService.CreateDefaultService();
  chromeDriverService.HideCommandPromptWindow = true;
  return new ChromeDriver(chromeDriverService, new ChromeOptions());
}

Also if you running console app, try to modify your application properties - Outputtype should be 'Window Application' instead 'Console Application'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM