我正在使用 Microsoft UI 自动化并且遇到了一些问题,一个是我想知道 AutomationElement 是否还活着。更准确地说,我想检查一个窗口是否已关闭。我认为这是这类问题最常见的情况,我尝试了不同的场景,最终得到了一个解决方案,我尝试访问元素的不同属性并查看它是否抛出 ElementNotAvailableException。我还偶然发现了一个名为 IsOffscreen 的属性,它在这种情况下似乎非常有用。但是,由于我在网上找不到太多关于此的信息,我想知道是否有更好的解决方案。最近几天我对这个框架不太满意,因为它对我来说似乎很不稳定(尤其是在寻找 AutomationElement 时)。
非常感谢
马塞尔
.net c# ui-automation microsoft-ui-automation automationelement
我从 pip 安装了最后一个 pywinauto 模块。
我不知道如何使用 Check()、UnCheck()、GetCheckState() 方法。
这是我非常简单的代码示例。
from pywinauto import application
# Start the madvr settings application.
app = application.Application()
app.start_(r'C:\Program Files\LAV Filters\x86\madVR\madHcCtrl.exe editLocalSettingsDontWait')
# Handle the madvr settings window.
madvr = app.window_(title_re="madVR.*")
# Enable the smooth motion tab.
madvr.TreeView.GetItem(r'\rendering\smooth motion').Click()
# Check the smooth motion checkbox.
madvr.TCheckBox.Check()
Run Code Online (Sandbox Code Playgroud)
如果我使用 Click() 方法,它会起作用,但这不是我想要的。
madvr.TCheckBox.Click()
Run Code Online (Sandbox Code Playgroud)
如果该复选框已被选中,则会取消选中它。
为什么我不能使用 Check() 方法?
我尝试使用 Uncheck() 和 GetCheckState() 方法,它们也不起作用。
我们一直在使用 MS UIA 框架,并注意到在 Windows 10 / .NET 4.6 中查找对象集合时出现的明显放缓。
在 Windows 10 / .NET 4.6 机器上测试 AutomationElement.FindAll() 时,与在 Windows 8.1 / .NET 4.5.1 机器上查找完全相同的元素相比,我们查找元素集合的平均时间大约长 3 到 5 倍. 我的测试是针对 WPF DataGrid 进行虚拟化(使用回收)并获取 DataGrid 每一行内的所有单元格。
在我们的 Win10 机器上,每次 FindAll 调用获取每行中的单元格大约需要 30 - 50 毫秒甚至更长的时间。在 Win8.1 机器上,大约需要 5 - 10 毫秒。我不知道为什么,但我不认为这个问题仅限于 DataGrid,因为我们的 FindAll() 调用没有什么特别之处。
//get grid
AutomationElement gridElement = AutomationElement.RootElement.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty, "dataGridAutomationId"));
//get all visible rows
AutomationElementCollection dataItems = gridElement.FindAll(TreeScope.Descendants,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));
foreach (AutomationElement dataItem in dataItems)
{
if …Run Code Online (Sandbox Code Playgroud) 我尝试ElementFromIAccessible通过以下方式使用方法:
System.Windows.Automation;
...
[DllImport("UIAutomationClient.dll")]
public static extern int ElementFromIAccessible(
IAccessible accessible,
int childId,
[In, Out, MarshalAs(UnmanagedType.IUnknown)] ref AutomationElement element);
Run Code Online (Sandbox Code Playgroud)
AutomationElement来自哪里System.Windows.Automation。
当我尝试这样称呼它时:
Win32.ElementFromIAccessible(this.Accessible, 0, ref automaitonElement);
Run Code Online (Sandbox Code Playgroud)
它失败了
"System.Runtime.InteropServices.MarshalDirectiveException" "无法打包参数 #3"
我如何以正确的方式映射它?
我正在尝试自动化第三方 Win32 应用程序,我想在定义的时间间隔捕获特定窗口的图形内容。我处于此的早期阶段,我目前正在尝试通过 C#使用Microsoft UI 自动化API 来完成客户端应用程序和外部应用程序之间的大部分交互。我现在可以让外部应用程序执行我希望它执行的操作,但现在我想从似乎是第三方所有者绘制的控件的特定窗口中捕获图形。我怎样才能做到这一点?我要捕获的窗口是此图像中红色矩形标记的窗口:
我有一个这样的实现,但它依赖于外部应用程序的 UI 位于顶部,这对我来说并不能保证,所以我更愿意找到更通用的东西。
var p = Process.Start("c:\myapp.exe");
var mainForm = AutomationElement.FromHandle(p.MainWindowHandle);
// "workspace" below is the window whose content I want to capture.
var workspace = mainForm.FindFirst(TreeScope.Descendents,
new PropertyCondition(AutomationElement.ClassNameProperty, "AfxFrameOrView70u"));
var rect = (Rect) workspace.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty);
using (var bmp = new Bitmap((int)rect.Width, (int)rect.Height))
{
using (var g = Graphics.FromImage(bmp))
{
g.CopyFromScreen((int)rect.Left, (int)rect.Top, 0, 0, new Size((int)rect.Width, (int)rect.Height));
bmp.Save(@"c:\screenshot.png", ImageFormat.Png);
}
}
Run Code Online (Sandbox Code Playgroud)
当自动化应用程序在顶部时,上面的工作足够好,但它只是盲目地复制矩形中的屏幕,所以我的代码受机器上运行的任何东西的支配,并可能覆盖我的应用程序的窗口。
我已经阅读了一些将WM_PRINT消息发送到窗口的建议。几个月前的这个问题/答案似乎很有希望,但是当我使用此代码时,我只得到一个白色矩形,其中没有控件的实际内容。
var prop = (int)workspace.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty);
var …Run Code Online (Sandbox Code Playgroud) 我是新来的.我正在使用UI Automation来自动化我的应用程序.有没有办法根据多个标识符识别元素.目前以下语法仅能够基于一个标识符进行识别.
AutomationElement okbtn = dialogbox.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "OK"));
Run Code Online (Sandbox Code Playgroud)
我想通过NameProperty和ControlTypeProperty获取标识元素.
这可能吗?
我们的应用程序有一个画布,我们可以在其中添加绘图视觉效果(如线条、多边形等)
// sample code
var canvas = new Canvas(); // create canvas
var visuals = new VisualCollection(canvas); // link the canvas to the visual collection
visuals.Add(new DrawingVisual()); // add the visuals to the canvas
visuals.Add(new DrawingVisual());
Run Code Online (Sandbox Code Playgroud)
我们的目标是通过自动化将这些视觉效果添加到画布中,并验证它们是否已正确添加。我们使用基于 Microsoft 的 UIAutomation 的框架。
当使用“Inspect”之类的工具检查视觉结构时,我无法找到画布。做了一些研究,发现您需要重写OnCreateAutomationPeer的方法UIElement,并返回适用的AutomationPeer对象,以便能够在自动化中看到它。
进行了更改,现在我可以看到画布,但是我仍然看不到画布下添加的任何视觉效果。
谁能帮我理解问题是什么?
尝试过的事情/替代方案:
DrawingVisuals 不是派生自UIElement,而且我无法将UIElements 添加到Canvas.VisualCollection。我正在致力于自动化 Windows 应用程序。我正在使用 teststack 白色框架。我遇到了问题。这个程序有一个我看不到里面的“窗口”对象。白色显示其中没有控件。在 UI 自动化模式下运行时,Inspect.exe 也不显示其中的任何控件。如果我将 Inspect 切换到 MSAA,它会看到里面的控件就好了。无论如何,我是否可以使用 C# 中的 MSAA 来处理这些控件?
我正在从事一个自动化项目,我需要在其中捕获用户在桌面上执行的活动 [启动的应用程序、输入的数据、输入类型等]。我遇到了 Microsoft UI 自动化框架,到目前为止,它适用于基于本机 Windows 的应用程序,如 MS Office、.NET 应用程序等。但是我没有找到任何有用的信息/从不同网络浏览器捕获信息的示例 [Chrome 是必须的] ,Python 应用程序,Java 应用程序等。有人可以确认 MS UI 自动化框架是否支持此类应用程序。任何从这些应用程序中提取用户活动的工作示例都将受到高度赞赏。谢谢。