【问题标题】:phantomjs is not closing the processphantomjs 没有关闭进程
【发布时间】:2015-10-19 09:49:26
【问题描述】:

我正在尝试在C# 中使用PhantomJS (v 2.0) 测试一些东西,但没有正确关闭它,这导致我的机器使用了 100% 的 CPU。

所有程序都运行良好,除了关闭它的部分。

我试过了

driver.Dispose();
driver.Quit();
driver.Close();

和(全部)

Enviroment.Exit(-1);
Enviroment.Exit(0);
Enviroment.Exit(1);
return;

但进程仍在任务管理器中

我试图粗暴地关闭控制台

 Process.GetCurrentProcess().Kill();

但仍然无法正常工作。

控制台中有幻影向我显示的消息

[INFO  - 2015-10-19T09:43:58.542Z] GhostDriver - Main - running on port 62810
[INFO  - 2015-10-19T09:44:17.480Z] Session [f7138220-7645-11e5-b667-c72dd4c4644c
] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true
,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"loc
alToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.1; WOW6
4) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.0.0 Safari/538.1","webSecur
ityEnabled":true}
[INFO  - 2015-10-19T09:44:17.499Z] Session [f7138220-7645-11e5-b667-c72dd4c4644c
] - page.customHeaders:  - {}
[INFO  - 2015-10-19T09:44:17.507Z] Session [f7138220-7645-11e5-b667-c72dd4c4644c
] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.0.0
","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-7-32bit
","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databas
eEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"
browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":f
alse,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"prox
yType":"direct"}}
[INFO  - 2015-10-19T09:44:17.530Z] SessionManagerReqHand - _postNewSessionComman
d - New Session Created: f7138220-7645-11e5-b667-c72dd4c4644c
[INFO  - 2015-10-19T09:45:27.620Z] ShutdownReqHand - _handle - About to shutdown

此外,我正在使用 selenium,但我认为没有重要信息。

有什么想法吗?

谢谢。

【问题讨论】:

  • 不幸的是,这似乎是一个常见问题。见thisthis

标签: c# phantomjs


【解决方案1】:

这是我使用 Selenium WebDriver 和 PhantomJS 解决这个问题的方法。

var service = PhantomJSDriverService.CreateDefaultService();
try
{
    driver = new PhantomJSDriver(service);
    driver.Navigate().GoToUrl("http://www.google.com");
}
catch (Exception ex)
{
    service.Dispose();
}
finally
{
    service.Dispose();
}

【讨论】:

    【解决方案2】:

    可能的解决方案

    我不会将其标记为有效,因为它是暂时的,但目前,解决问题的最简单/最快的方法是获取进程并手动终止它。

    为什么不是最好的解决方案,因为要杀死所有PhantomJS进程,在我的情况下只有一个,然后是有效的,但比锁定服务器要好。

    foreach (Process proc in Process.GetProcessesByName("PhantomJS"))
    {
        proc.Kill();
    }
    

    如果有人知道获得特定流程的方法(如果有多个),将受到欢迎。

    【讨论】:

      【解决方案3】:

      在这种情况下,这样的事情似乎效果很好:

              //Close Driver
          driver.Close();
      
          // Get the current process.
          System.Diagnostics.Process myProcess = System.Diagnostics.Process.GetCurrentProcess(); 
      
          // Close process by sending a close message to its main window.
          myProcess.CloseMainWindow();
      
          // Free resources associated with process.
          myProcess.Close();
      
          Environment.Exit(0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 2014-09-14
        • 1970-01-01
        • 1970-01-01
        • 2013-11-28
        相关资源
        最近更新 更多