0

我正在尝试通过 Python.NET 使用来自 FlaUI 接口的通用成员。

下面的 C# 代码有效,

// myLegacyTreeItem is a valid AutomationElement
// this works and the default action expands the tree node
myLegacyTreeItem.Patterns.LegacyIAccessible.Pattern.DoDefaultAction();

但是,下面相应的 Python.NET 代码不起作用,

# my_legacy_tree_item is a valid element
my_legacy_tree_item.Patterns.LegacyIAccessible.Pattern.DoDefaultAction()

我收到以下错误,

AttributeError: 'UIA3FrameworkAutomationElement' object has no attribute 'LegacyIAccessible'

我可以看到该LegacyIAccessible成员正在使用通用语法。但我无法弄清楚如何将它绑定到具体类型。

https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.Core/FrameworkAutomationElementBase.Patterns.cs

using FlaUI.Core.Patterns;

namespace FlaUI.Core
{
    public abstract partial class FrameworkAutomationElementBase : FrameworkAutomationElementBase.IFrameworkPatterns
    {
        private IAutomationPattern<IAnnotationPattern> _annotationPattern;
        private IAutomationPattern<IDockPattern> _dockPattern;
        private IAutomationPattern<IDragPattern> _dragPattern;
        private IAutomationPattern<IDropTargetPattern> _dropTargetPattern;
        private IAutomationPattern<IExpandCollapsePattern> _expandCollapsePattern;
        private IAutomationPattern<IGridItemPattern> _gridItemPattern;
        private IAutomationPattern<IGridPattern> _gridPattern;
        private IAutomationPattern<IInvokePattern> _invokePattern;
        private IAutomationPattern<IItemContainerPattern> _itemContainerPattern;
        private IAutomationPattern<ILegacyIAccessiblePattern> _legacyIAccessiblePattern;
        ...
        public IFrameworkPatterns Patterns => this;

        IAutomationPattern<IAnnotationPattern> IFrameworkPatterns.Annotation => _annotationPattern ?? (_annotationPattern = InitializeAnnotationPattern());
        IAutomationPattern<IDockPattern> IFrameworkPatterns.Dock => _dockPattern ?? (_dockPattern = InitializeDockPattern());
        IAutomationPattern<IDragPattern> IFrameworkPatterns.Drag => _dragPattern ?? (_dragPattern = InitializeDragPattern());
        IAutomationPattern<IDropTargetPattern> IFrameworkPatterns.DropTarget => _dropTargetPattern ?? (_dropTargetPattern = InitializeDropTargetPattern());
        IAutomationPattern<IExpandCollapsePattern> IFrameworkPatterns.ExpandCollapse => _expandCollapsePattern ?? (_expandCollapsePattern = InitializeExpandCollapsePattern());
        IAutomationPattern<IGridItemPattern> IFrameworkPatterns.GridItem => _gridItemPattern ?? (_gridItemPattern = InitializeGridItemPattern());
        IAutomationPattern<IGridPattern> IFrameworkPatterns.Grid => _gridPattern ?? (_gridPattern = InitializeGridPattern());
        IAutomationPattern<IInvokePattern> IFrameworkPatterns.Invoke => _invokePattern ?? (_invokePattern = InitializeInvokePattern());
        IAutomationPattern<IItemContainerPattern> IFrameworkPatterns.ItemContainer => _itemContainerPattern ?? (_itemContainerPattern = InitializeItemContainerPattern());
        IAutomationPattern<ILegacyIAccessiblePattern> IFrameworkPatterns.LegacyIAccessible => _legacyIAccessiblePattern ?? (_legacyIAccessiblePattern = InitializeLegacyIAccessiblePattern());
        ...
    }
}

如果我如下更改调用,则没有错误,但树项未展开,

((FrameworkAutomationElementBase.IFrameworkPatterns)(my_legacy_tree_item.Patterns)).LegacyIAccessible.Pattern.DoDefaultAction()

尝试使用下面的演员会抛出错误,

((FrameworkAutomationElementBase.IFrameworkPatterns[ILegacyIAccessiblePattern])(parent.Patterns)).LegacyIAccessible.Pattern.DoDefaultAction()
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at Python.Runtime.GenericUtil.GenericByName(String ns, String name, Int32 paramCount)
   at Python.Runtime.ClassBase.type_subscript(IntPtr idx)
4

1 回答 1

0

回答我自己的问题,因为这是一个愚蠢的错误。

下面的行确实扩展了遗留树项,

((FrameworkAutomationElementBase.IFrameworkPatterns)(my_legacy_tree_item.Patterns)).LegacyIAccessible.Pattern.DoDefaultAction()

但是,由于在我的情况下,我也立即右键单击同一项目,因此在展开树项目之前会弹出上下文菜单。因此sleep(1),在上述调用之后添加对我有用。

或者my_legacy_tree_item.DoubleClick()随后sleep(1)也工作过。

于 2022-02-08T03:52:12.923 回答