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
- Create the WebDriver by using
ChromeDriver(ChromeDriverService, ChromeOptions)
var service = ChromeDriverService.CreateDefaultService();
var webDriver = new ChromeDriver(service, options);
- 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)
- 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
Add Custom Headers to Webview Resource Requests - Android
How to Add a Recyclerview Inside Another Recyclerview
The Following Classes Could Not Be Instantiated: - Android.Support.V7.Widget.Toolbar
Illegalblocksizeexception When Trying to Encrypt and Decrypt a String with Aes
How to Get Unique Random Product in Node Firebase
How Set Background Drawable Programmatically in Android
Listview Onitemclicklistener Not Responding
Division in Java Always Results in Zero (0)
Change File Owner Group Under Linux with Java.Nio.Files
How to Run a Spring Boot Executable Jar in a Production Environment
How to Know Whether Enough Memory Is Free to Deploy a New Application on a Linux MAChine
How to Set Java_Home Path on Ubuntu
Problems with Java3D Lib Configuration
How to Build Netty-Transport-Native-Epoll-4.0.32.Final-Linux-X86_64.Jar