How to Get Chromedriver Process Pid Using Java

How can I get chromedriver process PID using Java?

You can find PIDs using pgrep and then kill it:

    private void killChromedriver() throws IOException, InterruptedException {
String command = "pgrep chromedriver";
Process process = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
List<String> processIds = getProcessedIds (process, br);
for (String pid: processIds) {
Process p = Runtime.getRuntime().exec("kill -9 " + pid);
p.waitFor();
p.destroy();
}
}
private List<String> getProcessedIds(Process process, BufferedReader br) throws IOException, InterruptedException {
process.waitFor();

List<String> result = new ArrayList<>();
String processId ;

while (null != (processId = br.readLine())) {
result.add(processId);
}

process.destroy();
return result;
}

UPDATE

Another and more simple solution seems to be

    Runtime.getRuntime().exec("pkill chromedriver");

Find PID of browser process launched by Selenium WebDriver

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

how to identify Chrome processes launched by ChromeDriver?

You will need to create some shared memory to identify process ids. MemoryMapped files could be one solution, database/WCF, named piped IPC etc.

That is because you can get the process id from your web driver by using ChromeDriverService

  1. Create the WebDriver by using ChromeDriver(ChromeDriverService, ChromeOptions)
var service = ChromeDriverService.CreateDefaultService();
var webDriver = new ChromeDriver(service, options);

  1. Get the processId from your service, or save the services so that they can be globally accessible in your application (by having some kind of singleton store or something like that)
  2. Update the shared memory or the communicating application by the stored in memory data

There is no straightforward way I can think that does not require quite an effort.



Related Topics



Leave a reply



Submit