本文介绍了Chrome .Net Ui自动化 - 自动登录浏览器 - AutomationElement Invoke似乎无法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下片段来自WPF:

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

命名空间Chrome_WPF_SSO
{

///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{

$ InvokePattern(InvokePattern.Pattern) ;


public MainWindow()
{
InitializeComponent();

// 1
var Chrome = Process.GetProcessesByName(chrome);
if(Chrome.Length
else
{
foreach(Chrome中的var cp)
{
if(!(cp.MainWindowHandle == IntPtr.Zero))
{

//要找到我们首先需要找到可靠的标签 - 'New Tab'按钮
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,将Chrome数据链接到此帐户?);
AutomationElementCollection allNames = rootElement.FindAll(TreeScope.Descendants,condCustom);
TreeWalker tWalker2 = TreeWalker.ControlViewWalker;

System.Windows.Automation.Condition LinkData = new PropertyCondition(AutomationElement.NameProperty,链接数据);
AutomationElementCollection elementscoll = rootElement.FindAll(TreeScope.Descendants,LinkData);
$ b $ foreach(AutomationElement a in elementscoll)
{

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



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

}

}
}


}


}
}
}
}

Win SDK inspect.exe工具:

 如何找到:从树中选择... 
名称:链接数据
ControlType:UIA_ButtonControlTypeId(0xC350)
LocalizedControlType:button
BoundingRectangle:{l:740 t:271 r:816 b:304}
IsEnabled:true
IsOffscreen:false
IsKeyboardFocusable:true
HasKeyboardFocus:false
AcceleratorKey:[不支持]
AccessKey:
ProcessId:4832
RuntimeId:[Not支持]
AutomationId:[不支持]
FrameworkId:[不支持]
类名:[不支持]
NativeWindowHandle:[不支持]
IsControlElement:[不支持]支持]
IsContentElement:[不支持
ProviderDescription:[pid:4832,hwnd:0x0 Main(parent link):Microsoft:MSAA Proxy(unmanaged:uiautomationcore.dll)]
IsPeripheral:[不支持]
AriaRole:[不支持]
AriaProperties:[不支持]
LiveSettingProperty:[不支持]
IsPassword:false
ItemStatus:[不支持]
ItemType:[不支持]
IsRequiredForForm:[不支持]
IsDataValidForForm:[不支持]
HelpText:LabelButton
LabeledBy:[不支持]
DescribedBy:[Not支持]
ControllerForProperty:[不支持]
FlowsTo:[不支持]
FlowsFrom:[不支持]
ClickablePoint:[不支持]
文化:[不支持]支持]
方向:[不支持]
OptimizeForVisualContent:[不支持]
Annotation.AnnotationAuthor:[不支持]
Annotation.AnnotationTypeId:[不支持]
Annotation.Author:[不支持]
Annotation.DateTime:[不是[不支持]
注解。
Drag.IsGrabbed:[不支持]
Dock.DockPosition:[不支持]
DropTarget.DropTargetEffect:[不支持]
DropTarget.DropTargetEffects:[不支持]
ExpandCollapse.ExpandCollapseState:[不支持]
Grid.ColumnCount:[不支持]
Grid.RowCount:[不支持]
GridItem.Column:[不支持]
GridItem.ColumnSpan:[不支持]
GridItem.ContainingGrid:[不支持]
GridItem.Row:[不支持]
GridItem.RowSpan:[不支持]
LegacyIAccessible。 ChildId:0
LegacyIAccessible.DefaultAction:
LegacyIAccessible.Description:
LegacyIAccessible.Help:LabelButton
LegacyIAccessible.KeyboardShortcut:
LegacyIAccessible。名称:链接数据
LegacyIAccessible.Role:按钮(0x2 B)
LegacyIAccessible.State:focusable(0x100000)
LegacyIAccessible.Value:[不支持]
MultipleView.CurrentView:[不支持]
MultipleView.SupportedViews:[不支持]
ObjectModel.UnderlyingObjectModel:[Error:为此属性调用getter:hr = 0xFFFFFFFF80070057 - 参数不正确]
RangeValue.IsReadOnly:[不支持]
RangeValue.LargeChange:[不支持]
RangeValue.Maximum:[不支持]
RangeValue.Minimum:[不支持]
RangeValue.SmallChange:[不支持]
RangeValue.Value:[不支持]
Selection.CanSelectMultiple:[不支持]
Selection.IsSelectionRequired:[不支持]
Selection.Selection:[不支持]
SelectionItem.IsSelected:[不支持]
SelectionItem.SelectionContainer:[不支持]
Scroll.Horizo​​ntalScrollable:[不支持]
Scroll.Horizo​​ntalScrollPercent:[不支持]
Scroll.Horizo​​ntalViewSize:[N ot supported]
Scroll.VerticallyScrollable:[不支持]
Scroll.VerticalScrollPercent:[不支持]
Scroll.VerticalViewSize:[不支持]
SpreadsheetItem.AnnotationObjects:[不支持] ]
SpreadsheetItem.AnnotationTypes:[不支持]
SpreadsheetItem.Formula:[不支持]
Style.ExtendedProperties:[不支持]
Style.FillColor:[不支持]
Style.FillPatternColor:[不支持]
Style.FillPatternStyle:[不支持]
Style.Shape:[不支持]
Style.StyleId:[不支持]
Style.StyleName:[不支持]
Value.IsReadOnly:[不支持]
Value.Value:[不支持]
Table.ColumnHeaders:[不支持]
表.ItemColumnHeaderItems:[不支持]
Table.ItemRowHeaderItems:[不支持]
Table.RowHeaders:[不支持]
Table.RowOrColumnMajor:[不支持]
Toggle.ToggleState :[不支持]
Transform.CanMov e:[不支持]
Transform.CanResize:[不支持]
Transform.CanRotate:[不支持]
Transform2.CanZoom:[不支持]
Transform2.ZoomLevel: [不支持]
Transform2.ZoomMinimum:[不支持]
Transform2.ZoomMaximum:[不支持]
Window.CanMaximize:[不支持]
Window.CanMinimize:[不支持]支持]
Window.IsModal:[不支持]
Window.IsTopmost:[不支持]
Window.WindowInteractionState:[不支持]
Window.WindowVisualState:[不支持]
IsAnnotationPatternAvailable:[不支持]
IsDragPatternAvailable:[不支持]
IsDockPatternAvailable:false
IsDropTargetPatternAvailable:[不支持]
IsExpandCollapsePatternAvailable:false
IsGridItemPatternAvailable: false
IsGridPatternAvailable:false
IsInvokePatternAvailable:true
IsItemContainerPatternAvailable:false
IsLegacyIAccessiblePatternAvailable:true
IsMul tipleViewPatternAvailable:false $ b $ IsObjectModelPatternAvailable:[不支持]
IsRangeValuePatternAvailable:false $ b $ IsScrollItemPatternAvailable:false $ b $ IsScrollPatternAvailable:false $ b $ IsSelectionItemPatternAvailable:false $ b $ IsSelectionPatternAvailable:false
IsSpreadsheetItemPatternAvailable:[不支持]
IsSpreadsheetPatternAvailable:[不支持]
IsStylesPatternAvailable:[不支持]
IsSynchronizedInputPatternAvailable:false
IsTableItemPatternAvailable:false
IsTablePatternAvailable:false $ $ b IsTextPatternAvailable:[不支持]
IsTextPatternAvailable:[不支持]
IsTransform2PatternAvailable:[不支持]
IsValuePatternAvailable:false
IsVirtualizedItemPatternAvailable:false
IsWindowPatternAvailable:false
FirstChild:image
LastChild:链接数据文本
下一个:[null]
上一个:取消按钮
其他道具:对象没有附加属性
儿童:图片
链接数据文本
祖先:

将Chrome数据链接到此帐户?
登录 - company.com Ltd - Google Chrome窗口
桌面
桌面窗口
[无父]


解决方案

你的代码对我来说似乎很好。
你有什么异常吗?请让我知道如果是这样的话。



如果您没有得到任何异常,Button不会以正确的方式实现InvokePattern。 (它实现的方式属于被测试的应用程序(AUT),并且更具体地指向) - 例如:提供程序告诉调用模式调用右键单击而不是左键单击后台,不能从外面改变。



解决方法如果您无法修改AUT strong>)是使用边界矩形或clickablePoint属性。将光标移动到此位置并模拟点击(例如,通过user32.dll)。注意你的窗口在前台(这限制了你的脚本并行执行几个应用程序!)

如果你能够改变AUT,最好的深入了解UI自动化提供程序文档或查找相关教程。


I would like to automatically sign into Chrome on Windows, the Selenium part is done but now there is a Window that needs to be automated. The invocation of the Link data push button. It appears I find the right element with my code and it does attempt an invoke but to no avail.

Below snippet is from 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);
                                }

                            }

                        }
                    }


                }


            }
        }
    }
}

From the Win SDK inspect.exe tool:

        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 ]
解决方案

Your code seems just fine for me.Do you get any exception? please let me know if so.

In case you don't get any exception, the Button does not implement the InvokePattern the right way. (The way it's implemented belongs to the application under test(AUT), and to be more specific to the actually UI Automation Provider of that element.) - e.g.: the provider tells the invoke pattern to invoke right-click instead of left-click in the background which sadly can not be changed from the outside.

A work-around on that (in case you can not modify the AUT) is to use the bounding rectangle or clickablePoint propertery. Move the cursor to this location and simulate a click (by "user32.dll" for example). Take care that your window is in the foreground (which limits your script in parallel execution of several applications!)

In case you are able to change the AUT, the best is to dig into the UI Automation Provider documentation or find some tutorial on that.

这篇关于Chrome .Net Ui自动化 - 自动登录浏览器 - AutomationElement Invoke似乎无法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:13