Selenium
sometimes fails to close after calling driver.quit()
when it gets stuck at "connection to server was reset" or when it simply "stops responding". When this occurs, it is impossible kill the process (browser) using WebDriver
directly, the only way I can think of is by retrieving the PID
of browser and destroying process via:
String cmd = "taskkill /F /PID " + pidOfBrowser;
Runtime.getRuntime().exec(cmd);
I'm aware of this response which suggests retrieving a list of processes currently running and filtering it down to Firefox browser. However, as someone pointed out in one of the comments this does not work if a user has many concurrent sessions running and only wishes to kill a select few.
Another solution suggested in the comment section of that thread is to get a list of PIDs from the browser before starting, and only close those that were not running before the test started (so any browser launched manually before tests began won't close)
However, this does not apply to my situation because I am launching many browsers at a time from my program (not manually) and only wish to close some of the browsers launched (the ones that are hanging and not responding to WebDriver
anymore).
How can I get PID
of a specific Firefox WebDriver
session (ideally when it is created) so I can kill the process later if it hangs or gets 'stuck'?
Thanks!
You can kill the Browser instance initiated using Selenium retriving the PID from the capabilities object and then invoking getRuntime()
with taskkill /PID
as follows:
Code Block:
import java.io.IOException;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Kill_Firefox_PID {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
System.out.println("moz:processID value is : "+cap.getCapability("moz:processID"));
Runtime.getRuntime().exec("taskkill /PID "+cap.getCapability("moz:processID"));
}
}
Console Output:
moz:processID value is : 8492
In Java 9+ version you can do like this. It is taken from here
public static void main(String[] args) {
ProcessHandle.allProcesses()
.forEach(process -> System.out.println(processDetails(process)));
}
private static String processDetails(ProcessHandle process) {
return String.format("%8d %8s %10s %26s %-40s",
process.pid(),
text(process.parent().map(ProcessHandle::pid)),
text(process.info().user()),
text(process.info().startInstant()),
text(process.info().commandLine()));
}
private static String text(Optional<?> optional) {
return optional.map(Object::toString).orElse("-");
}
and then get info
of the process like above. There is a totalCpuDuration()
method in ProcessHandle.Info
class. use this to see which process is taking long and then act accordingly. There are other methods in that class that can be beneficial in your scenario. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With