Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver PhantomJS C# always opens a cmd window

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.

like image 226
Vikas Ojha Avatar asked Dec 20 '13 20:12

Vikas Ojha


People also ask

Does Selenium support PhantomJS?

Just like any other browsers with GUI interface (Firefox, IE, Chrome, etc.), for PhantomJS also, Selenium has a standard API to support the automation. The above code snippet launches Selenium official website on the PhantomJS browser and performs click operation on the download tab.

Is PhantomJS faster than Selenium?

Being a headless browser, the interactions are much faster than the real browser. So the performance time is smoother in PhantomJS than in Selenium.

What is PhantomJS driver?

PhantomJS is a headless Webkit, which has a number of uses. In this example, we'll be using it, in conjunction with Selenium WebDriver, for conducting basic system tests directly from the command line. Since PhantomJS eliminates the need for a graphical browser, tests run much faster.


2 Answers

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);
like image 176
Martin Suchanek Avatar answered Oct 12 '22 23:10

Martin Suchanek


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.

like image 26
JimEvans Avatar answered Oct 13 '22 00:10

JimEvans