如何识别由ChromeDriver启动的Chrome进程?

huangapple go评论78阅读模式
英文:

how to identify Chrome processes launched by ChromeDriver?

问题

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

var driver = Browser.GetChromeDriver("P1");  // user profile 1
driver.Navigate().GoToUrl("https://google.com/");

Please note that code comments and variable names remain unchanged.

英文:

Browser.cs:

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

Run:

var driver = Browser.GetChromeDriver("P1");  // user profile 1
 driver.Navigate().GoToUrl("https://google.com/");

basically I build these small app to call up multiple Chrome instances, now I wonder if there's a way to identify the processes from another app, so in case I want to delete specified chrome processes that are launched by Profile 1 from Process.GetProcessesByName("chromedriver")

答案1

得分: 2

如果您想获取由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);

// 获取Chrome浏览器的PID和HWND详细信息

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

.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

.Id; CurrentBrowserHwnd = processes

.MainWindowHandle; break; } } CurrentBrowserHwnd应该包含您的Chrome窗口的HWND CurrentBrowserPID应该包含您的Chrome窗口的进程ID

英文:

If you want to get the identify the Chrome process that was launched by chromedriver then the following is a solution. This example is in C#. There would be the same implementation for other languages (like Java) but I am only working in C#.

Chrome allows you to supply your own user-defined command-line arguments. So you can add an argument named "scriptpid-" with the PID (Windows Process ID) of your currently running program. ChromeDriver passes your argument to Chrome in the command-line. Then using Windows WMI calls retrieve this PID from the command-line of the running Chrome ...

public static IntPtr CurrentBrowserHwnd = IntPtr.Zero;
public static int CurrentBrowserPID = -1;
		
ChromeOptions options = new ChromeOptions();
options.AddArgument(&quot;scriptpid-&quot; + 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(&quot;chrome&quot;);
for (int p = 0; p &lt; processes.Length; p++)
{
	ManagementObjectSearcher commandLineSearcher = new ManagementObjectSearcher(&quot;SELECT CommandLine FROM Win32_Process WHERE ProcessId = &quot; + processes

.Id); String commandLine = &quot;&quot;; foreach (ManagementObject commandLineObject in commandLineSearcher.Get()) { commandLine += (String)commandLineObject[&quot;CommandLine&quot;]; } String script_pid_str = (new Regex(&quot;--scriptpid-(.+?) &quot;)).Match(commandLine).Groups[1].Value; if (!script_pid_str.Equals(&quot;&quot;) &amp;&amp; Convert.ToInt32(script_pid_str).Equals(System.Diagnostics.Process.GetCurrentProcess().Id)) { CurrentBrowserPID = processes

.Id; CurrentBrowserHwnd = processes

.MainWindowHandle; break; } }

CurrentBrowserHwnd should contain the HWND of your Chrome window.

CurrentBrowserPID should contain the Process ID of your Chrome window.

答案2

得分: 0

你需要创建一些共享内存来识别进程ID。MemoryMapped files 可能是一种解决方案,也可以使用数据库/WCF、命名管道IPC等方式。

这是因为你可以通过使用 ChromeDriverService 从你的Web驱动程序中获取进程ID。

  1. 使用 ChromeDriver(ChromeDriverService, ChromeOptions) 创建WebDriver
var service = ChromeDriverService.CreateDefaultService();
var webDriver = new ChromeDriver(service, options);
  1. 从你的服务中获取进程ID,或者保存这些服务以便它们可以在你的应用程序中全局访问(通过某种单例存储或类似方式)
  2. 更新共享内存或存储在内存中的通信应用程序数据

我认为没有简单的方法,不需要相当大的努力。

英文:

You will need to create some shared memory to identify process ids. MemoryMapped files could be one solution, database/WCF, named piped IPC etc.

That is because you can get the process id from your web driver by using ChromeDriverService

  1. Create the WebDriver by using ChromeDriver(ChromeDriverService, ChromeOptions)
var service = ChromeDriverService.CreateDefaultService();
var webDriver = new ChromeDriver(service, options);
  1. Get the processId from your service, or save the services so that they can be globally accessible in your application (by having some kind of singleton store or something like that)
  2. Update the shared memory or the communicating application by the stored in memory data

There is no straightforward way I can think that does not require quite an effort.

huangapple
  • 本文由 发表于 2020年1月3日 16:14:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定