Chrome .Net Ui 自动化 - 自动登录到浏览器 - AutomationElement Invoke 似乎调用但没有结果

Chrome .Net Ui Automation - Automatic Sign on to Browser - AutomationElement Invoke appears to invoke with no result

我想在 Windows 上自动登录 Chrome,Selenium 部分已完成,但现在有一个 Window 需要自动化。 Link 数据按钮的调用。看来我在我的代码中找到了正确的元素,它确实尝试调用但无济于事。

以下代码段来自 WPF:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Automation;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Chrome_WPF_SSO
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public InvokePattern GetInvokePattern(AutomationElement element)
        {
            return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
        }

        public MainWindow()
        {
            InitializeComponent();

            //1
            var Chrome = Process.GetProcessesByName("chrome");
            if (Chrome.Length <= 0) { }

            else
            {
                foreach (var cp in Chrome)
                {
                    if (!(cp.MainWindowHandle == IntPtr.Zero))
                    {

                        //To find the tabs we first need to locate something reliable - the 'New Tab' button
                        AutomationElement rootElement = AutomationElement.FromHandle(cp.MainWindowHandle);
                        System.Windows.Automation.Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "Google Chrome");


                        System.Windows.Automation.Condition condCustom = new PropertyCondition(AutomationElement.NameProperty, "Link your Chrome data to this account?");
                        AutomationElementCollection allNames = rootElement.FindAll(TreeScope.Descendants, condCustom);
                        TreeWalker tWalker2 = TreeWalker.ControlViewWalker;

                        System.Windows.Automation.Condition LinkData = new PropertyCondition(AutomationElement.NameProperty, "Link data");
                        AutomationElementCollection elementscoll = rootElement.FindAll(TreeScope.Descendants, LinkData);

                        foreach (AutomationElement a in elementscoll)
                        {

                            if (a.Current.LocalizedControlType.ToString() == "button")
                            {
                                try
                                {
                                    ((InvokePattern)a.GetCurrentPattern(InvokePattern.Pattern)).Invoke();


                                }

                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }

                            }

                        }
                    }


                }


            }
        }
    }
}

来自 Win SDK inspect.exe 工具:

        How found:  Selected from tree...
Name:   "Link data"
ControlType:    UIA_ButtonControlTypeId (0xC350)
LocalizedControlType:   "button"
BoundingRectangle:  {l:740 t:271 r:816 b:304}
IsEnabled:  true
IsOffscreen:    false
IsKeyboardFocusable:    true
HasKeyboardFocus:   false
AcceleratorKey: [Not supported]
AccessKey:  ""
ProcessId:  4832
RuntimeId:  [Not supported]
AutomationId:   [Not supported]
FrameworkId:    [Not supported]
ClassName:  [Not supported]
NativeWindowHandle: [Not supported]
IsControlElement:   [Not supported]
IsContentElement:   [Not supported]
ProviderDescription:    "[pid:4832,hwnd:0x0 Main(parent link):Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]"
IsPeripheral:   [Not supported]
AriaRole:   [Not supported]
AriaProperties: [Not supported]
LiveSettingProperty:    [Not supported]
IsPassword: false
ItemStatus: [Not supported]
ItemType:   [Not supported]
IsRequiredForForm:  [Not supported]
IsDataValidForForm: [Not supported]
HelpText:   "LabelButton"
LabeledBy:  [Not supported]
DescribedBy:    [Not supported]
ControllerForProperty:  [Not supported]
FlowsTo:    [Not supported]
FlowsFrom:  [Not supported]
ClickablePoint: [Not supported]
Culture:    [Not supported]
Orientation:    [Not supported]
OptimizeForVisualContent:   [Not supported]
Annotation.AnnotationAuthor:    [Not supported]
Annotation.AnnotationTypeId:    [Not supported]
Annotation.Author:  [Not supported]
Annotation.DateTime:    [Not supported]
Annotation.Target:  [Not supported]
Drag.DropEffect:    [Not supported]
Drag.DropEffects:   [Not supported]
Drag.GrabbedItems:  [Not supported]
Drag.IsGrabbed: [Not supported]
Dock.DockPosition:  [Not supported]
DropTarget.DropTargetEffect:    [Not supported]
DropTarget.DropTargetEffects:   [Not supported]
ExpandCollapse.ExpandCollapseState: [Not supported]
Grid.ColumnCount:   [Not supported]
Grid.RowCount:  [Not supported]
GridItem.Column:    [Not supported]
GridItem.ColumnSpan:    [Not supported]
GridItem.ContainingGrid:    [Not supported]
GridItem.Row:   [Not supported]
GridItem.RowSpan:   [Not supported]
LegacyIAccessible.ChildId:  0
LegacyIAccessible.DefaultAction:    ""
LegacyIAccessible.Description:  ""
LegacyIAccessible.Help: "LabelButton"
LegacyIAccessible.KeyboardShortcut: ""
LegacyIAccessible.Name: "Link data"
LegacyIAccessible.Role: push button (0x2B)
LegacyIAccessible.State:    focusable (0x100000)
LegacyIAccessible.Value:    [Not supported]
MultipleView.CurrentView:   [Not supported]
MultipleView.SupportedViews:    [Not supported]
ObjectModel.UnderlyingObjectModel:  [Error: calling getter for this property: hr=0xFFFFFFFF80070057 - The parameter is incorrect.]
RangeValue.IsReadOnly:  [Not supported]
RangeValue.LargeChange: [Not supported]
RangeValue.Maximum: [Not supported]
RangeValue.Minimum: [Not supported]
RangeValue.SmallChange: [Not supported]
RangeValue.Value:   [Not supported]
Selection.CanSelectMultiple:    [Not supported]
Selection.IsSelectionRequired:  [Not supported]
Selection.Selection:    [Not supported]
SelectionItem.IsSelected:   [Not supported]
SelectionItem.SelectionContainer:   [Not supported]
Scroll.HorizontallyScrollable:  [Not supported]
Scroll.HorizontalScrollPercent: [Not supported]
Scroll.HorizontalViewSize:  [Not supported]
Scroll.VerticallyScrollable:    [Not supported]
Scroll.VerticalScrollPercent:   [Not supported]
Scroll.VerticalViewSize:    [Not supported]
SpreadsheetItem.AnnotationObjects:  [Not supported]
SpreadsheetItem.AnnotationTypes:    [Not supported]
SpreadsheetItem.Formula:    [Not supported]
Style.ExtendedProperties:   [Not supported]
Style.FillColor:    [Not supported]
Style.FillPatternColor: [Not supported]
Style.FillPatternStyle: [Not supported]
Style.Shape:    [Not supported]
Style.StyleId:  [Not supported]
Style.StyleName:    [Not supported]
Value.IsReadOnly:   [Not supported]
Value.Value:    [Not supported]
Table.ColumnHeaders:    [Not supported]
Table.ItemColumnHeaderItems:    [Not supported]
Table.ItemRowHeaderItems:   [Not supported]
Table.RowHeaders:   [Not supported]
Table.RowOrColumnMajor: [Not supported]
Toggle.ToggleState: [Not supported]
Transform.CanMove:  [Not supported]
Transform.CanResize:    [Not supported]
Transform.CanRotate:    [Not supported]
Transform2.CanZoom: [Not supported]
Transform2.ZoomLevel:   [Not supported]
Transform2.ZoomMinimum: [Not supported]
Transform2.ZoomMaximum: [Not supported]
Window.CanMaximize: [Not supported]
Window.CanMinimize: [Not supported]
Window.IsModal: [Not supported]
Window.IsTopmost:   [Not supported]
Window.WindowInteractionState:  [Not supported]
Window.WindowVisualState:   [Not supported]
IsAnnotationPatternAvailable:   [Not supported]
IsDragPatternAvailable: [Not supported]
IsDockPatternAvailable: false
IsDropTargetPatternAvailable:   [Not supported]
IsExpandCollapsePatternAvailable:   false
IsGridItemPatternAvailable: false
IsGridPatternAvailable: false
IsInvokePatternAvailable:   true
IsItemContainerPatternAvailable:    false
IsLegacyIAccessiblePatternAvailable:    true
IsMultipleViewPatternAvailable: false
IsObjectModelPatternAvailable:  [Not supported]
IsRangeValuePatternAvailable:   false
IsScrollItemPatternAvailable:   false
IsScrollPatternAvailable:   false
IsSelectionItemPatternAvailable:    false
IsSelectionPatternAvailable:    false
IsSpreadsheetItemPatternAvailable:  [Not supported]
IsSpreadsheetPatternAvailable:  [Not supported]
IsStylesPatternAvailable:   [Not supported]
IsSynchronizedInputPatternAvailable:    false
IsTableItemPatternAvailable:    false
IsTablePatternAvailable:    false
IsTextChildPatternAvailable:    [Not supported]
IsTextEditPatternAvailable: [Not supported]
IsTextPatternAvailable: false
IsTextPattern2Available:    [Not supported]
IsTogglePatternAvailable:   false
IsTransformPatternAvailable:    false
IsTransform2PatternAvailable:   [Not supported]
IsValuePatternAvailable:    false
IsVirtualizedItemPatternAvailable:  false
IsWindowPatternAvailable:   false
FirstChild: "" image
LastChild:  "Link data" text
Next:   [null]
Previous:   "Cancel" button
Other Props:    Object has no additional properties
Children:   "" image
    "Link data" text
Ancestors:  "" 
    "" 
    "Link your Chrome data to this account?" 
    "Log in - company.com Ltd - Google Chrome" window
    "Desktop" 
    "Desktop" window
    [ No Parent ]

你的代码似乎很适合我。 你有什么例外吗?如果是,请告诉我。

如果您没有收到任何异常,则说明 Button 没有以正确的方式实现 InvokePattern。 (它的实现方式属于被测应用程序(AUT),更具体地说是该元素的实际 UI Automation Provider。) - 例如:提供者告诉调用模式调用右键单击而不是左键单击-单击背景,遗憾的是无法从外部更改。

一个work-around如果你不能修改AUT)就是使用边界矩形或 clickablePoint 属性 。将光标移动到该位置并模拟单击(例如 "user32.dll")。请注意您的 window 在前台(这会限制您的脚本并行执行多个应用程序!)

如果您能够更改 AUT,最好是深入研究 UI Automation Provider 文档或查找相关教程。