【问题标题】:C# How to get tabs from chrome in multiple windows?C#如何在多个窗口中从chrome中获取标签?
【发布时间】:2017-06-17 09:24:53
【问题描述】:

所以我有 code 从 chrome 窗口中提取所有选项卡。

但是,如果我打开了多个窗口,则只能识别最近的一个。

是否可以从多个窗口中提取选项卡而不是仅从最近的一个窗口中提取?

编辑: 我使用并需要修复的代码:

public List<string> ChromeTabs()
    {
        List<string> ret = new List<string>();

        Process[] procsChrome = Process.GetProcessesByName("chrome");

        if (procsChrome.Length <= 0)
        {
            Console.WriteLine("Chrome is not running");
        }
        else
        {
            foreach (Process proc in procsChrome)
            {
                // the chrome process must have a window 

                if (proc.MainWindowHandle == IntPtr.Zero)
                {
                    continue;
                }

                // to find the tabs we first need to locate something reliable - the 'New Tab' button 
                AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
                Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
                AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);

                // get the tabstrip by getting the parent of the 'new tab' button 
                TreeWalker treewalker = TreeWalker.ControlViewWalker;
                AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab); // <- Error on this line

                // loop through all the tabs and get the names which is the page title 
                Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
                foreach (AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children, condTabItem))
                {
                    ret.Add(tabitem.Current.Name);
                    //Console.WriteLine(tabitem.Current.Name);
                }
                continue;
            }
        }
        return ret;
    }`

【问题讨论】:

  • 您其他问题的答案中的代码看起来是正确的,它正在枚举所有 chrome 进程(因为 chrome 是多进程),然后抓住窗口句柄。由于多进程的性质,我无法想象一个进程会有多个窗口。当您说“最近的”时,您是指前景中的那个,还是您与之交互的最后一个?
  • 最后一个交互的
  • 您能告诉我们您当前的代码吗?
  • @mjwills 该网站目前给了我 503 状态码,所以我稍后再试一次并回复您。

标签: c# google-chrome tabs window


【解决方案1】:

我也遇到了这个问题,只有最近使用的 Google Chrome 窗口返回非零 MainWindowHandle,这反过来又阻止了我从其他 Google Chrome 窗口获取标签信息。

我最终通过枚举桌面窗口并找到每个 Google Chrome 进程的 Handle 来解决此问题,这些进程遵守一组我发现(通过数小时的调试)可靠地返回浏览器窗口的规则。

我能够将在桌面窗口枚举期间发现的Handles 传递给典型的AutomationElement 方法,该方法用于获取运行Google Chrome 窗口的所有 的标签信息。

delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);

[DllImport("user32.dll")]
private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumWindowsProc ewp, int lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

[DllImport("user32.dll")]
private static extern uint GetWindowText(IntPtr hWnd, StringBuilder lpString, uint nMaxCount);

[DllImport("user32.dll")]
private static extern uint GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

// get all the process id's of the running chrome processes
Process[] chromeProcesses = Process.GetProcessesByName("chrome");
List<uint> chromeProcessIds = chromeProcesses.Select(x => (uint)x.Id).ToList();

// a list to store the handles of the Google Chrome windows that we'll find
List<IntPtr> windowHandles = new List<IntPtr>();

EnumWindowsProc enumerateHandle = delegate (IntPtr hWnd, int lParam)
{
    // get the id of the process of the window we are enumerating over
    uint id;
    GetWindowThreadProcessId(hWnd, out id);

    // if the process we're enumerating over has an id in our chrome process ids, we need to inspect it to see if it is a window or other process
    if (chromeProcessIds.Contains(id))
    {
        // get the name of the class of the window we are inspecting
        var clsName = new StringBuilder(256);
        var hasClass = GetClassName(hWnd, clsName, 256);
        if (hasClass)
        {
            // get the text of the window we are inspecting
            var maxLength = (int)GetWindowTextLength(hWnd);
            var builder = new StringBuilder(maxLength + 1);
            GetWindowText(hWnd, builder, (uint)builder.Capacity);

            var text = builder.ToString();
            var className = clsName.ToString();

            // actual Google Chrome windows have text set to the title of the active tab
            // in my testing, this needs to be coupled with the class name equaling "Chrome_WidgetWin_1". 
            // i haven't tested this with other versions of Google Chrome
            if (!string.IsNullOrWhiteSpace(text) && className.Equals("Chrome_WidgetWin_1", StringComparison.OrdinalIgnoreCase))
            {
                // if we satisfy the conditions, this is a Google Chrome window. Add the handle to the list of handles to use later.
                windowHandles.Add(hWnd);
            }
        }
    }
    return true;
};

EnumDesktopWindows(IntPtr.Zero, enumerateHandle, 0);

foreach (IntPtr ptr in windowHandles)
{
    AutomationElement root = AutomationElement.FromHandle(ptr);
   
    // continue grabbing Chrome tab information using the AutomationElement method
    ...
}

【讨论】:

  • 我调试了很长一段时间,直到我找到了这个答案,效果很好!我想知道为什么它还没有得到任何支持
  • 单位应该是 uint 的小错字
  • @Joe 谢谢 - 我编辑了答案并修正了错字。
猜你喜欢
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多