问题描述
在C#中,我启动了一个浏览器进行测试,我想获取PID,以便在Winforms应用程序中可以杀死启动的所有剩余Ghost进程
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();
如何获取PID?
推荐答案
看起来更像是C#问题,而不是特定于硒.
Looks more like a C# question, instead of Selenium specific.
我的逻辑是,使用firefox的所有进程PID > Process.GetProcessesByName方法,然后启动FirefoxDriver
,然后再次获取进程的PID,比较它们以获取刚刚启动的PID.在这种情况下,特定驱动程序已经启动了多少个进程都没有关系(例如,Chrome启动多个,Firefox仅启动一个).
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();
}
}
}
}
这篇关于查找由Selenium WebDriver启动的浏览器进程的PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!