Chrome浏览器多开后,如何逐个激活窗口

背景:
多开Chrome浏览器,各自独立,开了10个。
现在需要影刀去获取当前:已打开的网页对象

这里就涉及到,需要有个程序,循环间隔,去逐个激活Chrome浏览器
我想通过C#写个程序,访问任务栏,把某个Chrome浏览器激活。
请各位指教,有没有好的办法。

如下图,把其中一个浏览器窗口,变为当前窗口,这样影刀,就可以通过:获取当前已打开的网页对象 ,做相关数据处理。

img

在 C# 中,你可以使用 System.Diagnostics.Process 类来实现激活特定的 Chrome 浏览器窗口。以下是一个简单的示例代码,用于激活指定标题的 Chrome 浏览器窗口:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    static void Main()
    {
        // 指定需要激活的 Chrome 浏览器标题
        string targetTitle = "Google Chrome";

        // 获取当前所有进程中的 Chrome 浏览器窗口
        Process[] processes = Process.GetProcessesByName("chrome");

        foreach (var process in processes)
        {
            if (process.MainWindowTitle == targetTitle)
            {
                // 激活指定标题的 Chrome 浏览器窗口
                SetForegroundWindow(process.MainWindowHandle);
                break;
            }
        }
    }
}

在上述代码中,我们通过 Process.GetProcessesByName("chrome") 获取所有正在运行的 Chrome 进程,然后遍历每个进程的 MainWindowTitle 属性(即窗口标题),找到与指定标题相符的 Chrome 浏览器窗口。然后,我们使用 SetForegroundWindow 方法将其激活(置于前台)。
请注意,这个方法只会激活 Chrome 浏览器的窗口,但并不会切换到指定的标签页或页面。如果需要激活浏览器窗口后再操作浏览器中的页面,你可能需要使用其他工具或库来实现在浏览器页面上执行操作。

  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7753643
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:Chrome 插件开发-右键菜单开发实战演示,浏览器页面右键菜单选项设置,插件右键菜单点击插件名跳转主页设置
  • 除此之外, 这篇博客: Chrome浏览器如何设置中文翻译中的 如果你已经打开这个chrome网页自动翻译功能,但是打开外文网页时如果没有自动弹出提示信息,可以在页面右击鼠标,会发现有 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 以下回答由chatgpt基于相关博客总结生成:

    在C#中使用任务栏激活Chrome浏览器窗口并获取当前已打开的网页对象,可以通过以下步骤实现:

    1. 引用System.Windows.Automation命名空间来使用任务栏的功能。在Visual Studio中,右击项目,选择“引用”,然后在搜索框中搜索并添加UIAutomationClientUIAutomationTypes

    2. 使用Automation类的ElementFromHandle方法来获取特定Chrome窗口的句柄。可以通过遍历任务栏上的窗口按钮来找到相应的按钮,并根据按钮的句柄获取对应的窗口句柄。

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows.Automation;
    
    public class ChromeWindowUtils
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
    
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
        public static void ActivateChromeWindow(IntPtr chromeHandle)
        {
            ShowWindow(chromeHandle, 9); // SW_RESTORE
    
            AutomationElement targetWindow = AutomationElement.FromHandle(chromeHandle);
            targetWindow.SetFocus();
        }
    
        public static IntPtr GetChromeWindowHandle(int tabNumber)
        {
            IntPtr taskbarHandle = FindWindow("Shell_TrayWnd", null);
            IntPtr startButtonHandle = FindWindowEx(taskbarHandle, IntPtr.Zero, "Button", "Start");
            IntPtr taskbarMdiHandle = FindWindowEx(taskbarHandle, startButtonHandle, "MSTaskListWClass", null);
    
            IntPtr chromeButtonHandle = FindWindowEx(taskbarMdiHandle, IntPtr.Zero, "Button", null);
            int currentTabNumber = 1;
    
            do
            {
                if(currentTabNumber == tabNumber)
                {
                    return chromeButtonHandle;
                }
    
                chromeButtonHandle = FindWindowEx(taskbarMdiHandle, chromeButtonHandle, "Button", null);
                currentTabNumber++;
            }
            while(chromeButtonHandle != IntPtr.Zero);
    
            return IntPtr.Zero;
        }
    }
    
    // Example usage
    IntPtr chromeHandle = ChromeWindowUtils.GetChromeWindowHandle(3); // Get handle of the 3rd chrome window
    ChromeWindowUtils.ActivateChromeWindow(chromeHandle); // Activate the chrome window
    
    1. 通过使用AutomationElement类的FromHandle方法,可以将窗口句柄转换为AutomationElement对象。然后使用SetFocus方法来激活窗口。

    以上代码会根据给定的tabNumber参数来获取特定的Chrome窗口句柄,并激活该窗口。如果没有找到相应的窗口,将返回IntPtr.Zero

    此外,这里使用了ShowWindow函数来还原最小化的窗口,确保窗口处于可见状态。

    请注意,此方法是基于任务栏按钮的位置和顺序来获取窗口句柄的,如果Chrome窗口在任务栏上的顺序和按钮的位置不匹配,可能会导致获取到错误的窗口句柄。在实际使用中,请根据需要调整代码来确保获取正确的窗口句柄。

    在获取到特定窗口的句柄后,您可以继续使用其他方法来与Chrome浏览器交互,例如使用FindFirst方法和FindAll方法来查找并获取网页对象的元素,或者使用Chrome浏览器的开发者工具来获取网页对象的相关信息。