查找Selenium WebDriver启动的浏览器进程的PID

在C#中我启动了一个浏览器进行测试,我想得到PID,这样在我的winforms应用程序中我可以杀死所有剩余的ghost进程

driver = new FirefoxDriver(); 

我怎样才能获得PID?

看起来更像是一个C#问题,而不是Selenium特有的。

这是一个非常古老的非确定性答案,如果您想尝试一下,请重新考虑。

我的逻辑是你使用Process.GetProcessesByName方法获取名为firefox所有进程PID,然后启动你的FirefoxDriver ,然后再次获取进程的PID,比较它们以获得刚启动的PID。 在这种情况下,特定驱动程序启动了多少进程并不重要(例如,Chrome启动多个,Firefox只启动一个)。

 using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium.Firefox; namespace TestProcess { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { IEnumerable pidsBefore = Process.GetProcessesByName("firefox").Select(p => p.Id); FirefoxDriver driver = new FirefoxDriver(); IEnumerable pidsAfter = Process.GetProcessesByName("firefox").Select(p => p.Id); IEnumerable newFirefoxPids = pidsAfter.Except(pidsBefore); // do some stuff with PID if you want to kill them, do the following foreach (int pid in newFirefoxPids) { Process.GetProcessById(pid).Kill(); } } } } 

尝试使用父进程ID:

  public static Process GetWindowHandleByDriverId(int driverId) { var processes = Process.GetProcessesByName("chrome") .Where(_ => !_.MainWindowHandle.Equals(IntPtr.Zero)); foreach (var process in processes) { var parentId = GetParentProcess(process.Id); if (parentId == driverId) { return process; } } return null; } private static int GetParentProcess(int Id) { int parentPid = 0; using (ManagementObject mo = new ManagementObject($"win32_process.handle='{Id}'")) { mo.Get(); parentPid = Convert.ToInt32(mo["ParentProcessId"]); } return parentPid; } 
 var g = Guid.NewGuid(); driver.Navigate().GoToUrl("about:blank"); driver.ExecuteScript($"document.title = '{g}'"); var pid = Process.GetProcessesByName("firefox").First(p => p.MainWindowTitle.Contains(g.ToString())); 

我没有尝试使用Firefox,但这是适用于Chrome的方式:

  // creating a driver service var driverService = ChromeDriverService.CreateDefaultService(); _driver = new ChromeDriver(driverService); //create list of process id var driverProcessIds = new List { driverService.ProcessId }; //Get all the childs generated by the driver like conhost, chrome.exe... var mos = new System.Management.ManagementObjectSearcher($"Select * From Win32_Process Where ParentProcessID={driverService.ProcessId}"); foreach (var mo in mos.Get()) { var pid = Convert.ToInt32(mo["ProcessID"]); driverProcessIds.Add(pid); } //Kill all foreach (var id in driverProcessIds) { System.Diagnostics.Process.GetProcessById(id).Kill(); } 

要获取进程ID并尝试终止它,您将使用Process类。 兴趣方法是:

  • GetProcessesByName

按名称解析进程后,您可以查询其属性: process.Id以获取ID。 请注意,某些浏览器(如Google Chrome)有多个正在运行的流程(Chrome每个标签至少有一个正在运行的流程)。 因此,如果你想要杀死它,你需要杀死所有进程。

开箱即用,selenium不公开驱动程序进程ID或浏览器hwnd,但它是可能的。 以下是获得hwnd的逻辑

  • 初始化驱动程序时,获取集线器的URL并提取端口号
  • 从端口号,找到使用此端口进行监听的进程ID,即。 驱动程序的PID
  • 导航后,从iexplore的所有实例中找到父PID匹配驱动程序的pid,即浏览器pid。
  • 找到浏览器hwnd后得到浏览器pid的Hwnd,你可以使用win32 api将selenium带到前台。

它不可能在这里发布完整代码,将浏览器放在前面的完整工作解决方案(C#)在我的博客上

http://www.pixytech.com/rajnish/2016/09/selenium-webdriver-get-browser-hwnd/

  int _processId = -1; var cService = ChromeDriverService.CreateDefaultService(); cService.HideCommandPromptWindow = true; // Optional var options = new ChromeOptions(); options.AddArgument("--headless"); IWebDriver webdriver = new ChromeDriver(cService, options); _processId = cService.ProcessId; Console.Write("Process Id : " + _processId); webdriver.Navigate().GoToUrl("https://www.google.lk"); webdriver.Close(); webdriver.Quit(); webdriver.Dispose();