Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find PID of browser process launched by Selenium WebDriver

In C# I start up a browser for testing, I want to get the PID so that on my winforms application I can kill any remaining ghost processes started

driver = new FirefoxDriver();

How can I get the PID?

like image 863
user1320651 Avatar asked Sep 08 '13 17:09

user1320651


4 Answers

Looks more like a C# question, instead of Selenium specific.

This is a very old non-deterministic answer, please reconsider if you want to try this out.

My logic would be you get all process PIDs with the name firefox using Process.GetProcessesByName Method, then start your FirefoxDriver, then get the processes' PIDs again, compare them to get the PIDs just started. In this case, it doesn't matter how many processes have been started by a specific driver (For example, Chrome starts multiple, Firefox only one).

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();
            }
        }
    }
}
like image 54
Yi Zeng Avatar answered Oct 10 '22 19:10

Yi Zeng


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()));
like image 24
W0nd3r Avatar answered Oct 10 '22 17:10

W0nd3r


I didn't try with Firefox, but that is the way that works 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();
        }
like image 40
juanora Avatar answered Oct 10 '22 17:10

juanora


        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();
like image 38
Gehan Fernando Avatar answered Oct 10 '22 18:10

Gehan Fernando