C# 如何识别ChromeDriver启动的Chrome流程?
Browser.cs:C# 如何识别ChromeDriver启动的Chrome流程?,c#,selenium,selenium-chromedriver,C#,Selenium,Selenium Chromedriver,Browser.cs: public static ChromeDriver GetChromeDriver(string machine) { String chromeLocalAppDataPath = GetChromeLocations(machine); //"d:\ChromeTest\Google\Chrome\User Data\Auto\"; var headless = true; var options = n
public static ChromeDriver GetChromeDriver(string machine)
{
String chromeLocalAppDataPath = GetChromeLocations(machine); //"d:\ChromeTest\Google\Chrome\User Data\Auto\";
var headless = true;
var options = new ChromeOptions();
options.AddArgument("--no-experiments");
options.AddArgument("disable-infobars");
options.AddArgument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
if (headless)
options.AddArgument("headless");
options.AddArgument("no-sandbox");
options.AddArguments("user-data-dir=" + chromeLocalAppDataPath);
options.BinaryLocation = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
return new ChromeDriver(options);
}
运行:
基本上,我构建这些小应用程序是为了调用多个Chrome实例,现在我想知道是否有办法从另一个应用程序中识别流程,所以,如果我想从Process.getProcessByNameChromeDriver中删除配置文件1启动的指定chrome进程,则需要创建一些共享内存来标识进程ID。可以是一个解决方案,数据库/WCF,命名为管道IPC等 这是因为您可以使用 使用ChromeDriverService、ChromeOptions创建Web驱动程序 从您的服务中获取processId,或者保存服务,以便通过某种单例存储或类似的方式在应用程序中全局访问它们 通过存储在内存中的数据更新共享内存或通信应用程序
我认为没有一种简单的方法不需要付出很大的努力。如果您想获得识别chromedriver启动的Chrome进程的信息,那么下面是一个解决方案。这个例子是用C语言编写的。其他语言(如Java)也有同样的实现,但我只使用C语言编写 Chrome允许您提供自己的用户定义的命令行参数。因此,您可以添加一个名为scriptpid的参数,该参数具有当前运行程序的PID Windows进程ID。ChromeDriver在命令行中将您的参数传递给Chrome。然后使用Windows WMI调用从运行Chrome的命令行检索此PID
public static IntPtr CurrentBrowserHwnd = IntPtr.Zero;
public static int CurrentBrowserPID = -1;
ChromeOptions options = new ChromeOptions();
options.AddArgument("scriptpid-" + System.Diagnostics.Process.GetCurrentProcess().Id);
IWebDriver driver = new ChromeDriver(options);
// Get the PID and HWND details for a chrome browser
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("chrome");
for (int p = 0; p < processes.Length; p++)
{
ManagementObjectSearcher commandLineSearcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + processes[p].Id);
String commandLine = "";
foreach (ManagementObject commandLineObject in commandLineSearcher.Get())
{
commandLine += (String)commandLineObject["CommandLine"];
}
String script_pid_str = (new Regex("--scriptpid-(.+?) ")).Match(commandLine).Groups[1].Value;
if (!script_pid_str.Equals("") && Convert.ToInt32(script_pid_str).Equals(System.Diagnostics.Process.GetCurrentProcess().Id))
{
CurrentBrowserPID = processes[p].Id;
CurrentBrowserHwnd = processes[p].MainWindowHandle;
break;
}
}
CurrentBrowserHwnd应该包含Chrome窗口的HWND
CurrentBrowserPID应该包含Chrome窗口的进程ID
var service = ChromeDriverService.CreateDefaultService();
var webDriver = new ChromeDriver(service, options);
public static IntPtr CurrentBrowserHwnd = IntPtr.Zero;
public static int CurrentBrowserPID = -1;
ChromeOptions options = new ChromeOptions();
options.AddArgument("scriptpid-" + System.Diagnostics.Process.GetCurrentProcess().Id);
IWebDriver driver = new ChromeDriver(options);
// Get the PID and HWND details for a chrome browser
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("chrome");
for (int p = 0; p < processes.Length; p++)
{
ManagementObjectSearcher commandLineSearcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + processes[p].Id);
String commandLine = "";
foreach (ManagementObject commandLineObject in commandLineSearcher.Get())
{
commandLine += (String)commandLineObject["CommandLine"];
}
String script_pid_str = (new Regex("--scriptpid-(.+?) ")).Match(commandLine).Groups[1].Value;
if (!script_pid_str.Equals("") && Convert.ToInt32(script_pid_str).Equals(System.Diagnostics.Process.GetCurrentProcess().Id))
{
CurrentBrowserPID = processes[p].Id;
CurrentBrowserHwnd = processes[p].MainWindowHandle;
break;
}
}