Selenium Webdriver PhantomJS C# always opens a cmd window

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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.

回答1:

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.



回答2:

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);


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!