【问题标题】:getting chrome tabs in the correct order以正确的顺序获取 chrome 标签
【发布时间】:2016-02-24 10:00:59
【问题描述】:

我正在尝试以与浏览器标签栏中列出的顺序相同的顺序获取 chrome 标签列表。

这是我的代码:

var automationElements = AutomationElement.FromHandle(proc.MainWindowHandle);

// Find `New Tab` element
var propCondNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
var elemNewTab = automationElements.FindFirst(TreeScope.Descendants, propCondNewTab);

// Get parent of `New Tab` element
var treeWalker = TreeWalker.ControlViewWalker;
var elemTabStrip = treeWalker.GetParent(elemNewTab);

// Loop through all tabs
var tabItemCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
foreach (AutomationElement tabItem in elemTabStrip.FindAll(TreeScope.Children, tabItemCondition)) {
    var nameProperty = tabItem.GetCurrentPropertyValue(AutomationElement.NameProperty);
    Debug.WriteLine("title: " + nameProperty.ToString());
}

但是,在 foreach 循环中,标题的顺序与它们在浏览器中的顺序不同。有没有办法让它们按正确的顺序排列?

【问题讨论】:

    标签: c# google-chrome


    【解决方案1】:

    我不知道获取选项卡顺序的直接方法。您可以做的是获取选项卡矩形的位置,并按矩形在 X 轴上的位置对选项卡进行排序。

    一个非常简单的示例,我使用SortedDictionary 来保存选项卡及其 X 值。之后,循环遍历字典中的键并提取选项卡项。因为字典是排序的,键是按顺序排列的,所以列表将按照标签在浏览器中显示的顺序。

    SortedDictionary<double, AutomationElement> orderedTabItems = new SortedDictionary<double, AutomationElement>();
    
    // Loop through all tabs
    var tabItemCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
    foreach (AutomationElement tabItem in elemTabStrip.FindAll(TreeScope.Children, tabItemCondition))
    {
        Rect rectangleProperty = (Rect)tabItem.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty);
        orderedTabItems.Add(rectangleProperty.X, tabItem);
    }
    
    for(int i = 0; i < orderedTabItems.Keys.Count; i++)
    {
        var key = orderedTabItems.Keys.ElementAt(i);
        var tabItem = orderedTabItems[key];
        var nameProperty = tabItem.GetCurrentPropertyValue(AutomationElement.NameProperty);
        Debug.WriteLine("index: " + i + ", title: " + nameProperty.ToString());
    }
    

    我确信有更好的解决方案。这是我第一次尝试使用System.Windows.Automation

    编辑:我忘了说,要访问矩形的X 属性,您需要添加对WindowsBase.dll 的引用并在代码中引用它:

    using System.Windows;
    

    【讨论】:

    • 这很好,但就像我写的那样,这是我第一次尝试使用System.Windows.Automation,所以可能有一种更简洁的方法。老实说,通过使用@stamhaney 建议的方法,它“感觉正确”,从page tab list 获取选项卡,而不是调查 UI 矩形位置:-)
    【解决方案2】:

    您可以使用 Microsoft Active Accessibility (MSAA) 获得正确的选项卡顺序。此方法不像其他答案那样依赖屏幕坐标。以下是您需要做的:

    1. Chrome_WidgetWin_1 类和标题“New Tab - Google Chrome”获取 Chrome 的顶部窗口可访问对象
    2. 获取角色“page tab list”的 Accessible 对象
    3. 为第 2 点找到的对象获取 Accessible Children
    4. 遍历子列表并显示 accName 属性。

    有一个很好的article 可以帮助您开始使用 MSAA

    【讨论】:

    • 您能详细说明一下吗?我已经坚持你的第一点:D
    • @gartenriese,在 MSAA 上添加了更多信息和链接
    • 在您的链接中,他们通过调用GetTopWindowAccessibleList 获取列表中的顶部窗口可访问对象,但从未解释过该功能。我怎样才能获得这些可访问的对象?
    猜你喜欢
    • 2020-10-15
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 2013-05-27
    • 2010-09-25
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多