通过PID进程杀死Selenium Browser [Java] [英] Kill Selenium Browser by PID Process [Java]

查看:257
本文介绍了通过PID进程杀死Selenium Browser [Java]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Selenium卡在重置到服务器的连接"或仅停止响应"时,调用driver.quit()后有时无法关闭.发生这种情况时,不可能直接使用WebDriver杀死进程(浏览器),我能想到的唯一方法是检索浏览器的PID并通过以下方式破坏进程:

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

我知道了此响应,该响应建议检索当前正在运行的进程列表并将其过滤到Firefox浏览器.但是,正如某人在其中一项评论中指出的那样,如果用户正在运行多个并发会话,并且只希望杀死少数几个会话,那么这将不起作用.

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.

该线程的注释部分中建议的另一种解决方案是在启动之前从浏览器获取PID列表,并仅关闭测试开始之前未运行的PID(因此,在测试开始之前手动启动的任何浏览器都不会关闭)

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).

如何获取特定Firefox WebDriver会话的PID(理想情况下是在创建会话时),以便稍后在进程挂起或卡住时可以终止该进程?

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'?

谢谢!

推荐答案

您可以杀死使用 功能 对象,然后使用taskkill /PID调用getRuntime() :

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
    

  • 这篇关于通过PID进程杀死Selenium Browser [Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆