9

I am trying to use PhantomJS with Selenium Webdriver in C#. Following is my code:

IWebDriver driver = new PhantomJSDriver();
driver.Navigate().GoToUrl("http://www.google.com");
Console.WriteLine(driver.Url);
driver.Quit();

The code works fine but whenever it runs, it opens up a cmd window where all the log of the phantomjs is displayed. The cmd is also closed with driver.Quit().

The problem is that I do not want the cmd window to be displayed. What should I do to achieve this?

Update: When I do the same code in Python, the cmd window does not show up. But if I convert the python script to exe using py2exe, the cmd windows starts getting displayed again.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Vikas Ojha
  • 6,742
  • 6
  • 22
  • 35
  • I do not think you can disable the command window, sorry. – Brian Dec 20 '13 at 20:44
  • Thanks Brian and @user1177636. Actually we are creating a desktop software which will interact with many websites in the background and we want to look it as professional as we can. That is why we want that the command windows is not displayed. – Vikas Ojha Dec 20 '13 at 21:19

2 Answers2

34

As JimEvans mentions above, this feature was added in 2.40:

https://code.google.com/p/selenium/source/detail?r=bd0e4ef7504cd6a7f184b19b2aa95b56f8958ab5

I'm not exactly sure how to properly use PhantomJSDriverService, but the following works:

var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var driver = new PhantomJSDriver(driverService);
Martin Suchanek
  • 3,006
  • 6
  • 31
  • 31
  • Really appreciate the code for how to use this! I was really scratching my head on how to use the information from source code revision link. – bojingo Jul 29 '15 at 18:16
2

No, there is no way to hide the console window of the PhantomJS.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 PhantomJSDriver, since the console window remains open. In the case of some other languages, if your code does not properly clean up the instance of PhantomJSDriver by calling the quit() method on the WebDriver object, you can end up with a zombie PhantomJS.exe process running on your machine.

JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • Thanks @JimEvans for the detailed response. Could you help me out with the changes that I could do in the bindings source code. Also, is it the Selenium Webdriver bindings or the PhantomJS.exe which opens up the console window? – Vikas Ojha Dec 21 '13 at 18:21
  • 3
    Actually, the ability to hide this window has [just been added](http://code.google.com/p/selenium/source/detail?r=bd0e4ef7504cd6a7f184b19b2aa95b56f8958ab5) to the .NET bindings. It should be available when 2.40 is released. And before you ask, no there is no published timeline for when such a release would take place. – JimEvans Dec 23 '13 at 15:25
  • Thanks @JimEvans. It is going to help me a lot whenever it is released. – Vikas Ojha Dec 23 '13 at 15:51
  • Selenium 2.40 is already out for Python but I dont find this being recorded in changelog. Is it being postponed for future release? – Vikas Ojha Feb 21 '14 at 07:44
  • 2
    The .NET bindings 2.40 are available on NuGet. We are a little hamstrung at the moment in that we do not have a place to host the downloads for .NET and Java since Google Code has stopped hosting them. We are investigating solutions and expect to have it worked out soon. – JimEvans Feb 21 '14 at 12:07