问题
Description
I am trying to be able to interact with context menus using UI Automation. Basically, I am trying to:
- set focus on an
AutomationElement
SendKeys.SendWait
to send aSHIFT+F10
- see what pops up
What I Am Seeing
What I am seeing is that the AutomationElement.FindAll(TreeScope.Descendants, Condition.TrueCondition)
does not seem to reflect when the context menu is popped, even though UISpy sees it.
Any help would be greatly appreciated.
Example
Here is an example application that I have been running in LINQPad:
void Main()
{
var notepad = FindNotepad();
Console.WriteLine("Pre-Context: {0}", Descendants(notepad).Count);
TypeInto(notepad, "(+{F10})");
Thread.Sleep(1000);
Console.WriteLine("With Context: {0}", Descendants(notepad).Count);
TypeInto(notepad, "{ESC}");
Console.WriteLine("Post-Context: {0}", Descendants(notepad).Count);
}
AutomationElement FindNotepad(string title = "Untitled - Notepad")
{
var notepadName = new PropertyCondition(AutomationElement.NameProperty, title);
return AutomationElement.RootElement.FindFirst(TreeScope.Children, notepadName);
}
void TypeInto(AutomationElement element, string keys)
{
element.SetFocus();
SendKeys.SendWait(keys);
}
AutomationElementCollection Descendants(AutomationElement element)
{
return element.FindAll(TreeScope.Subtree, Condition.TrueCondition);
}
回答1:
The context menu that opened on Shift-F10 is actually a child of the root element (desktop) so if you use Descendants(AutomationElement.RootElement).Count
instead of Descendants(notepad).Count
you will see the difference.
e.g.
Pre-Context: 2019
With Context: 2036
Post-Context: 2019
来源:https://stackoverflow.com/questions/21889806/automationelement-context-menus