c# - 如何使用 UI 自动化激活 Google Chrome 标签项

标签 c# google-chrome ui-automation

我正在使用 C# 应用程序中的此代码在 Google Chrome 中查找选项卡:

        Process[] procsChrome = Process.GetProcessesByName("chrome");
        foreach (Process chrome in procsChrome)
        {
            // the chrome process must have a window
            if (chrome.MainWindowHandle == IntPtr.Zero)
            {
                continue;
            }

            AutomationElement root = AutomationElement.FromHandle(chrome.MainWindowHandle);
            /*
            Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "Nueva pestaña");
            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);
             */
            // 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 root.FindAll(TreeScope.Descendants, condTabItem))
            {
                Console.WriteLine(tabitem.Current.Name);

                // I NEED TO ACTIVATE THE TAB HERE

                break;
            }

            Condition condUrl = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit);
            foreach (AutomationElement edit in root.FindAll(TreeScope.Descendants, condUrl))
            {
                string value = ((System.Windows.Automation.ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value;
                Console.WriteLine(value);
            }

        }

我需要使用 UI 自动化选择某些选项卡项。我该怎么做?

最佳答案

对于那些绝望的灵魂,仍在寻找答案。这是我的方法,完全基于 UI 自动化 API,没有聚焦窗口和发送点击事件或热键。要使用下面的代码,您需要使用 UIAutomationCore.dll 的互操作引用,如 Guy Barker 所述。 .

    Process[] allChromeProcesses = Process.GetProcessesByName("chrome");
    Process[] mainChromes = allChromeProcesses.Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)).ToArray();
    //...
    //Here you need to check if you have found correct chrome instance
    //...
    var uiaClassObject = new CUIAutomation();

    IUIAutomationElement chromeMainUIAElement = uiaClassObject.ElementFromHandle(mainChromes[0].MainWindowHandle);
    //UIA_ControlTypePropertyId =30003, UIA_TabItemControlTypeId = 50019
    IUIAutomationCondition chromeTabCondition = uiaClassObject.CreatePropertyCondition(30003, 50019); 
    var chromeTabCollection = chromeMainUIAElement.FindAll(TreeScope.TreeScope_Descendants, chromeTabCondition);
    //UIA_LegacyIAccessiblePatternId = 10018, 0 -> Number of Chrome tab you want to activate
    var lp = chromeTabCollection.GetElement(0).GetCurrentPattern(10018) as IUIAutomationLegacyIAccessiblePattern;
    lp.DoDefaultAction();

您唯一需要记住的是,不可能在标签页中搜索最小化的 Chrome 窗口。

关于c# - 如何使用 UI 自动化激活 Google Chrome 标签项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39164128/

相关文章:

c# - 如何在使用http请求读取json文件时在monotouch中显示进度条

c# - 如何获取TextBlock的Text(TextBlock是Button的内容)

html - 浏览器兼容性 - DIV 在 Chrome 和 FF 中不同

javascript - 在 Chrome 上触发键盘事件

css - Chrome 用户代理 border-radius 覆盖了我的选择样式

ui-automation - TreeWalker 看不到 AutomationElement,但 UIA verify 可以

c# - 解析包含数组的字符串

c# - 从几个重叠的 png 中创建一个 jpeg

c++ - 在 Gecko 2.0 (Firefox 4) 中,您如何与当前未获得焦点的选项卡交互(向其发送 Windows 消息)?

java - 无法访问 Selenium 中的元素(缩放和滚动不起作用)