[英]Microsoft ui-automation not able to fetch chrome's context menu elements
为什么UIAutomation无法获取chrome的上下文菜单元素。
C#代码:以下代码将预订根元素。
public void SubscribeToInvoke()
{
Automation.AddAutomationEventHandler(AutomationElement.MenuOpenedEvent,
AutomationElement.RootElement,
TreeScope.Descendants, UIAEventHandler);
Automation.AddAutomationEventHandler(AutomationElement.MenuClosedEvent,
AutomationElement.RootElement,
TreeScope.Descendants, UIAEventHandler);
}
在使用谷歌浏览器的情况下,波纹管事件不会被触发,但是在其他情况下(例如IE或Firefox或任何其他应用程序)也可以。
private void UIAEventHandler(object sender, AutomationEventArgs e)
{
AutomationElement sourceElement;
sourceElement = sender as AutomationElement;
if (e.EventId == AutomationElement.MenuOpenedEvent)
{
}
else if (e.EventId == AutomationElement.MenuClosedEvent)
{
}
}
是否需要任何代码更改或对此问题有其他替代解决方案?
我已经通过使用称为FromPoint()的方法实现了此任务。 我的用例是获取右键单击和粘贴事件。
步骤1:订阅菜单打开和关闭事件:
public void SubscribeToInvoke()
{
Automation.AddAutomationEventHandler(AutomationElement.MenuOpenedEvent,
AutomationElement.RootElement,
TreeScope.Descendants, UIAEventHandler);
Automation.AddAutomationEventHandler(AutomationElement.MenuClosedEvent,
AutomationElement.RootElement,
TreeScope.Descendants, UIAEventHandler);
}
第2步:当MenuOpenedEvent启动计时器时,它将获取鼠标的当前位置以及MenuCloseEvent Strop计时器。
if (e.EventId == AutomationElement.MenuOpenedEvent)
{
timer_Chrome.Enabled = true;
}
else if (e.EventId == AutomationElement.MenuClosedEvent)
{
timer_Chrome.Enabled = false;
}
步骤3:在滑鼠位置取得元素
System.Windows.Point point = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y);
AutomationElement sourceElement = AutomationElement.FromPoint(point);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.