看起来更像是C#问题,而不是特定于Selenium。
这是一个非常古老的不确定性答案,如果您想尝试此方法,请重新考虑。
我的逻辑是,
firefox使用Process.GetProcessesByName方法获取具有名称的所有进程PID
,然后启动
FirefoxDriver,然后再次获取进程的PID,比较它们以获取刚刚启动的PID。在这种情况下,特定驱动程序已启动多少个进程无关紧要(例如,Chrome启动多个进程,Firefox仅启动一个进程)。
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(); } } }}


