【问题标题】:Get next element of AutomationElement获取 AutomationElement 的下一个元素
【发布时间】:2016-07-19 17:11:56
【问题描述】:

我正在尝试在 Chrome(或其他浏览器)中找到第一个音频播放选项卡。

我已经设法获取了所有打开的 chrome 标签的标题(主要使用来自 Getting the current tab's URL from Google Chrome using C#Get chrome browser title using c# 的信息)。

但是仅仅寻找包含单词 “Youtube” 的标签似乎有点奇怪(因为只要找到单词,它也会找到链接的问题或其他“非音乐内容”优酷)。

有没有办法找到第一个音频播放标签?

编辑
修复了初始问题:

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

        if(chromeProcesses.Length > 0)
        {
            foreach(Process proc in chromeProcesses)
            {
                if (proc.MainWindowHandle == IntPtr.Zero)
                {
                    continue;
                }

                AutomationElement chrome = AutomationElement.FromHandle(proc.MainWindowHandle);

                var tabs = chrome.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
                if (tabs != null)
                {
                    foreach(AutomationElement tab in tabs)
                    {
                        string tabTitle = tab.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
//...

我目前正在标签标题中检查“- YouTube”(在字符串的末尾)。但是有一种方法可以变得更加精确。通过获取标签文本的下一个元素。

使用inspect.exe 工具,我发现“当前播放”选项卡在文本旁边有一个空按钮(实际上是扬声器图标) - 否则它是一个关闭按钮。因此,通过检查下一个元素,应该可以找出选项卡是否也在播放音乐。

但我目前正在努力寻找下一个元素。如何使用 AutomationElement 类做到这一点?

【问题讨论】:

  • 您可以使用 chrome.tabs.query (developer.chrome.com/extensions/tabs#method-query) 从 Chrome 扩展程序中查询可听标签。使用 C#,虽然可能有一种使用各种 Pinvokes 的方法,但这将是一个脆弱的解决方案,因为 Google 可能随时更改他们的设计。
  • 不应该找到AutomationElement类的标签吗?

标签: c#


【解决方案1】:

修复一切:

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

        if(chromeProcesses.Length > 0)
        {
            foreach(Process proc in chromeProcesses)
            {
                if (proc.MainWindowHandle == IntPtr.Zero)
                {
                    continue;
                }

                AutomationElement chrome = AutomationElement.FromHandle(proc.MainWindowHandle);

                var tabs = chrome.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
                if (tabs != null)
                {
                    foreach(AutomationElement tab in tabs)
                    {
                        string tabTitle = tab.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();

                        AutomationElement next = tab.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));
                        if (next != null)
                        {
                            string nextContent = next.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
                            if(nextContent != "")
                            {
                                break;
                            }
                        }

                        if (tabTitle.LastIndexOf(YoutubeRecogPattern, StringComparison.OrdinalIgnoreCase) > 0)
                        {
                            if (tabTitle != this.LastTitle)
                            {
                                string videoTitle = tabTitle.Substring(0, tabTitle.Length - YoutubeRecogPattern.Length);
// ...

YoutubeRecogPattern 在哪里 - YouTube

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多