Find the PID of a browser running Selenium WebDriver

In C #, I launch a browser for testing, I want to get a PID so that in my winforms application I can kill any remaining running ghost processes

driver = new FirefoxDriver(); 

How can I get the PID?

+4
source share
6 answers

Looks like a C # question, not Selenium.

My logic would be that you get all the PID processes of a process called firefox using the Process.GetProcessesByName Method , then start your FirefoxDriver , then load the PID processes again, compare them to only start the PID. In this case, it does not matter how many processes were started by a particular driver (for example, Chrome starts several, only 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<int> pidsBefore = Process.GetProcessesByName("firefox").Select(p => p.Id); FirefoxDriver driver = new FirefoxDriver(); IEnumerable<int> pidsAfter = Process.GetProcessesByName("firefox").Select(p => p.Id); IEnumerable<int> 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(); } } } } 
+8
source

Try using the parent process 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; } 
+1
source
 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())); 
+1
source

To get the process ID and try to kill it, you must use the Process class. Interest Methods:

  • GetProcessesByName
  • To kill

Once you allow a process by name, you can request its property: process.Id for the identifier. Keep in mind that some browsers, such as Google Chrome, have several running processes (Chrome has at least one running process for each tab). Therefore, if you want to kill him, you need to kill all the processes.

0
source

Out of the box, selenium does not detect the process id or hwnd browser, but it is possible. Below is the logic for getting hwnd

  • When the driver is initialized, get the hub URL and extract the port number
  • From the port number, find the identifier of the process that this port uses to listen, i.e. PID driver.
  • After navigating from all instances of iexplore, find the parent PID corresponding to the driver pid, for example, pid pis.
  • Get hwnd browser pid after detecting hwnd browser, you can use win32 api to bring selenium to the foreground.

its impossible to publish the full code here, the full working solution (C #) for outputting the browser in front is in my blog

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

0
source

I have not tried Firefox, but it works like this with Chrome:

  // creating a driver service var driverService = ChromeDriverService.CreateDefaultService(); _driver = new ChromeDriver(driverService); //create list of process id var driverProcessIds = new List<int> { 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(); } 
0
source

Source: https://habr.com/ru/post/1501190/


All Articles