首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chrome .Net Ui自动化-自动登录到浏览器- AutomationElement调用似乎没有结果调用

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

Stack Overflow用户
提问于 2015-02-12 12:27:58
回答 1查看 3.4K关注 0票数 0

我想在Windows上自动登录Chrome,Selenium部分已经完成,但是现在有一个窗口需要自动化。链接数据按钮的调用。看来,我用代码找到了正确的元素,它确实尝试了调用,但没有结果。

下面的片段来自WPF:

代码语言:javascript
复制
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工具:

代码语言:javascript
复制
        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 ]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-13 13:47:59

你的代码对我来说很好。你有什么例外吗?如果是的话请告诉我。

如果您没有得到任何异常,Button没有以正确的方式实现InvokePattern。(它的实现方式属于测试中的应用程序(AUT),并且更具体地适用于该元素的实际UI自动化提供者。)例如:提供程序告诉调用模式调用右键单击,而不是在后台单击左键,可惜不能从外部更改。

如果您不能修改AUT,则使用边界矩形或clickablePoint属性。将光标移动到此位置并模拟单击(例如,通过"user32.dll“)。注意您的窗口在前台(这限制了您的脚本并行执行几个应用程序!)

如果您能够更改AUT,最好的方法是深入了解UI自动化提供程序的文档,或者找到有关这方面的教程。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28477479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档