WPF context menu
Hi, in the WPF application I'm trying to test, I'm struggling trying to click on a MenuItem inside a Context menu. The context menu is shown after a button is pressed. I'm able to press the button, but the PageSource doesn't contain the Context menu list item. How can I force a refresh of the PageSource?
Then I tried some tricks plying with the windows handlers, and then I'm able to get an updated PageSource with the ItemList that I want to click on, but then an error is raised (element not found) using GetElementByXPath (the only way that I have, no unique ID).
Any thoughts? Thank you
Hi @rallets,
with the ItemList that I want to click on, but then an error is raised (element not found) using GetElementByXPath (the only way that I have, no unique ID). Can you share additional information on the error that you are encountering + WinAppDriver logs?
Can you also try using a desktop session?
Hi, the error is "An element could not be located on the page using the given search parameters.". I tried with both, single xpath element, and multiple xpath elements: POST /session/A3B83948-A09D-4D15-AAD3-941433A2EB46/elements HTTP/1.1 Accept: application/json, image/png Content-Length: 58 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723 {"using":"xpath","value":"//MenuItem[@Name='Aktiebolag']"} HTTP/1.1 200 OK Content-Length: 74 Content-Type: application/json {"sessionId":"A3B83948-A09D-4D15-AAD3-941433A2EB46","status":0,"value":[]}
POST /session/A3B83948-A09D-4D15-AAD3-941433A2EB46/element HTTP/1.1 Accept: application/json, image/png Content-Length: 58 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723 {"using":"xpath","value":"//MenuItem[@Name='Aktiebolag']"} HTTP/1.1 404 Not Found Content-Length: 139 Content-Type: application/json {"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}
I have tried with a Desktop session, but same result. This the code I have used:
[...] The WPF app is started and the login is working fine... [...]
DesiredCapabilities wpfCapabilities = new DesiredCapabilities();
wpfCapabilities.SetCapability("app", "Root");
var DesktopSession = new WindowsDriver
var WpfWindow = DesktopSession.FindElementByName("Capxxx"); var WpfTopLevelWindowHandle = WpfWindow.GetAttribute("NativeWindowHandle"); WpfTopLevelWindowHandle = (int.Parse(WpfTopLevelWindowHandle)).ToString("x");
// Create session by attaching to Wpf top level window
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("appTopLevelWindow", WpfTopLevelWindowHandle);
var WpfSession = new WindowsDriver
This is the PageSource AFTER the context menu is shown:
If you need more info please just let me know. It is very important to us to be able to automate this scenario. Thank you.
Hi @rallets,
Have you tried to use the FindElementByClassName method like WpfSession.FindElementByClassName("Aktiebolag") to see if it will yield a different result?
Alternatively, you can try locating the pop up first and then search for the menu item within it. popup = WpfSession.FindElementByXpath("//Window[@ClassName='Popup']"); popup.FindElementsByXPath("//MenuItem[@name='Aktiebolag']");
Hi @nikolavn @hassanuz , I tried all suggestions, but unfortunately... no luck. I can do everything you want/ask to help in solving this problem, just let me know what I can do. Thank you for you support
Hi @rallets,
Can you try
WpfSession.FindElementsByXPath("//MenuItem[@Name='Aktiebolag']");
with a capital "N" in "Name" - you originally had it lower case.
Hi, I tried to parse the xml directly from an xpath tool. using the tool the PageSource contains the element as expected, but it founds 2 elements, so I have tried with FindElementsByXPath, but it returns 0 elements. It looks like a bug. Could it be? Attached the original xml and the screenshots from the online tool. The tool is www.freeformatter.com/xpath-tester.html
Can I do something else to have this working?
Thank you
Hi @hassanuz thanks for the suggestion, it was just a misspelling, see my previous post
Hi @hassanuz I have installed the latest version of WinAppDriver and I have tried with the new - and very useful - tool WAD UiRecorder. The generated xpath is: /Pane[@Name="Desktop 1"][@ClassName="#32769"]/Window[@Name="Capego"][@ClassName="Window"]/Window[@ClassName="Popup"]/MenuItem[@Name="Aktiebolag"][@ClassName="MenuItem"]/Text[@Name="Aktiebolag"][@ClassName="TextBlock"]
and is still not working. BTW just FYI I need to remove the first part "/Pane[@Name="Desktop 1"][@ClassName="#32769"]" to make it working on others elements, as example from /Pane[@Name="Desktop 1"][@ClassName="#32769"]/Window[@Name="Capego"][@ClassName="Window"]/Window[@ClassName="Popup"]/MenuItem[@Name="Aktiebolag"][@ClassName="MenuItem"]/Text[@Name="Aktiebolag"][@ClassName="TextBlock"]
to
//Window[@Name="Capego"][@ClassName="Window"]/Window[@ClassName="Popup"]/MenuItem[@Name="Aktiebolag"][@ClassName="MenuItem"]/Text[@Name="Aktiebolag"][@ClassName="TextBlock"]
Sometimes I get this error when it try to do the lookup for this specific element that is not working:

My impression is that WAD is not able to do the lookup on the latest "version" of the xml, so the xpath lookup fails. This due to the runtime generated popup that appear on a button-click.
Hope this helps. Thank you
Thanks for reporting this @rallets, we'll have to investigate further to see if this can be resolved in a future release.
Hi rallets,
When you tried using the desktop session, it looks like you were still attached to the WPF window. The context menu that you are seeing is a child of the desktop session instead of the application, and because of that it would be under the Desktop Session. So in this case, you would have to switch back to the desktop session and search for the element under it.
HI @hassanuz thank you for your feedback. Unfortunately I cannot test it anymore, but I will forward this to my colleague that is working on this project now.
Hi @hassanuz @hassanuz. Ive made a small wpf app that isolates our problem. The code consists of a button that shows a dropdownmenu. When sending a click event for the button, the menu is shown for a brief moment. Im doing a xml dump of the current loaded page, both before and after the click but cant seem to get the elements for the dropdownmenu. Also looking for addidional windowhandler, just in case the dropdown menu would initiate a new one. I know this is silly :/
Note. Im using the selenium.webdriver 3.10. Maybe the new version 3.14 has solved this issue, but as the DesiredCapabilities are depricated for the new version I havent found any solution for setting capabilities for the WindowsDriver. It looks like the new way of setting capabilities is only for browser spesific drivers. Could anyone provide an example?
Sourcecode: https://github.com/knut79/TestWpfDropdownMenu Build the solution and debug the UnitTest1. break at line 38
Thanks for the repro app @knut79, we ran it on Windows 10 (version 1803), and were unable to repro the same issue as you. We were able to locate the element and generate the following XML:
<?xml version="1.0" encoding="utf-16"?>
<Window AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Window" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="window" Name="MainWindow" Orientation="None" ProcessId="19116" RuntimeId="42.70446" x="0" y="0" width="460" height="347" CanMaximize="True" CanMinimize="True" IsModal="False" WindowVisualState="Normal" WindowInteractionState="ReadyForUserInteraction" IsTopmost="False" CanRotate="False" CanResize="True" CanMove="True" IsAvailable="True">
<Window AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Popup" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="window" Name="" Orientation="None" ProcessId="19116" RuntimeId="42.136004" x="3" y="201" width="277" height="110">
<Menu AcceleratorKey="" AccessKey="" AutomationId="" ClassName="ContextMenu" FrameworkId="WPF" HasKeyboardFocus="True" HelpText="" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="menu" Name="" Orientation="None" ProcessId="19116" RuntimeId="7.19116.51945267" x="3" y="201" width="277" height="110">
<MenuItem AcceleratorKey="" AccessKey="" AutomationId="ItemOne" ClassName="MenuItem" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="menu item" Name="Menu 1" Orientation="None" ProcessId="19116" RuntimeId="7.19116.64854219" x="11" y="207" width="251" height="44">
<Text AcceleratorKey="" AccessKey="" AutomationId="" ClassName="TextBlock" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="text" Name="Menu 1" Orientation="None" ProcessId="19116" RuntimeId="7.19116.38336904" x="81" y="213" width="81" height="32" />
</MenuItem>
<MenuItem AcceleratorKey="" AccessKey="" AutomationId="ItemTwo" ClassName="MenuItem" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="menu item" Name="Menu 2" Orientation="None" ProcessId="19116" RuntimeId="7.19116.46817064" x="11" y="251" width="251" height="44">
<Text AcceleratorKey="" AccessKey="" AutomationId="" ClassName="TextBlock" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="text" Name="Menu 2" Orientation="None" ProcessId="19116" RuntimeId="7.19116.9487824" x="81" y="257" width="81" height="32" />
</MenuItem>
<Button AcceleratorKey="" AccessKey="" AutomationId="LineUp" ClassName="RepeatButton" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="True" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="" Orientation="None" ProcessId="19116" RuntimeId="7.19116.18700393" x="-109" y="-98" width="0" height="0" />
<Button AcceleratorKey="" AccessKey="" AutomationId="LineDown" ClassName="RepeatButton" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="True" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="" Orientation="None" ProcessId="19116" RuntimeId="7.19116.34085817" x="-109" y="-98" width="0" height="0" />
</Menu>
</Window>
<TitleBar AcceleratorKey="" AutomationId="TitleBar" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="title bar" Orientation="None" ProcessId="19116" RuntimeId="42.70446.3.-2147483647.70446.-2.0" x="34" y="3" width="424" height="55">
<MenuBar AcceleratorKey="" AccessKey="Alt+Space" AutomationId="SystemMenuBar" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="menu bar" Name="System" Orientation="None" ProcessId="19116" RuntimeId="42.70446.3.-2147483646.19116.1114239.0" x="2" y="13" width="44" height="44">
<MenuItem AcceleratorKey="" AccessKey="Space" AutomationId="" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="menu item" Name="System" Orientation="None" ProcessId="19116" RuntimeId="42.70446.3.-2147483646.19116.1114239.1" x="2" y="13" width="44" height="44" ExpandCollapseState="Collapsed" IsAvailable="True" />
</MenuBar>
<Button AcceleratorKey="" AutomationId="Minimize-Restore" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="Minimize" Orientation="None" ProcessId="19116" RuntimeId="42.70446.3.-2147483647.70446.-2.2" x="179" y="1" width="94" height="57" />
<Button AcceleratorKey="" AutomationId="Maximize-Restore" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="Maximize" Orientation="None" ProcessId="19116" RuntimeId="42.70446.3.-2147483647.70446.-2.3" x="273" y="1" width="92" height="57" />
<Button AcceleratorKey="" AutomationId="Close" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="Close" Orientation="None" ProcessId="19116" RuntimeId="42.70446.3.-2147483647.70446.-2.5" x="365" y="1" width="94" height="57" />
</TitleBar>
<Button AcceleratorKey="" AccessKey="" AutomationId="TheMenuButton" ClassName="Button" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="Click Me" Orientation="None" ProcessId="19116" RuntimeId="7.19116.11244347" x="86" y="138" width="194" height="63">
<Text AcceleratorKey="" AccessKey="" AutomationId="" ClassName="TextBlock" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="text" Name="Click Me" Orientation="None" ProcessId="19116" RuntimeId="7.19116.34090260" x="138" y="154" width="90" height="31" />
</Button>
</Window>
Is this not what you're seeing?
@hassanuz thanks for the reply. Im also using Windows 10 v1803. Just not have any misunderstandings. Are you able to run the UnitTest1.cs -> TestMethod1() without any implications ? The xml you are showing, is that collected from the DumpXmlPageSource() called from the TestMethod1()?
My xml dump both before click event on the menubutton and after is totally differnet than yours. Im getting the following:
<?xml version="1.0" encoding="utf-16"?>
<Window AcceleratorKey="" AccessKey="" AutomationId="" ClassName="Window" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="window" Name="MainWindow" Orientation="None" ProcessId="26984" RuntimeId="42.399100" x="0" y="0" width="227" height="172" CanMaximize="True" CanMinimize="True" IsModal="False" WindowVisualState="Normal" WindowInteractionState="ReadyForUserInteraction" IsTopmost="False" CanRotate="False" CanResize="True" CanMove="True" IsAvailable="True">
<TitleBar AcceleratorKey="" AutomationId="" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="title bar" Orientation="None" ProcessId="26984" RuntimeId="42.399100.3.-2147483647.399100.-2.0" x="17" y="3" width="209" height="28">
<MenuBar AcceleratorKey="" AccessKey="Alt+Space" AutomationId="MenuBar" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="menu bar" Name="System" Orientation="None" ProcessId="26984" RuntimeId="42.399100.3.-2147483646.26984.1179670.0" x="1" y="8" width="22" height="22">
<MenuItem AcceleratorKey="" AccessKey="Space" AutomationId="" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="menu item" Name="System" Orientation="None" ProcessId="26984" RuntimeId="42.399100.3.-2147483646.6068.1179670.1" x="1" y="8" width="22" height="22" ExpandCollapseState="Collapsed" IsAvailable="True" />
</MenuBar>
<Button AcceleratorKey="" AutomationId="" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="Minimize" Orientation="None" ProcessId="26984" RuntimeId="42.399100.3.-2147483647.399100.-2.2" x="87" y="1" width="47" height="30" />
<Button AcceleratorKey="" AutomationId="" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="Maximize" Orientation="None" ProcessId="26984" RuntimeId="42.399100.3.-2147483647.399100.-2.3" x="134" y="1" width="46" height="30" />
<Button AcceleratorKey="" AutomationId="" ClassName="" FrameworkId="" HasKeyboardFocus="False" IsContentElement="False" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="Close" Orientation="None" ProcessId="26984" RuntimeId="42.399100.3.-2147483647.399100.-2.5" x="180" y="1" width="47" height="30" />
</TitleBar>
<Button AcceleratorKey="" AccessKey="" AutomationId="TheMenuButton" ClassName="Button" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="True" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="button" Name="Click Me" Orientation="None" ProcessId="26984" RuntimeId="7.26984.34090260" x="43" y="71" width="94" height="28">
<Text AcceleratorKey="" AccessKey="" AutomationId="" ClassName="TextBlock" FrameworkId="WPF" HasKeyboardFocus="False" HelpText="" IsContentElement="True" IsControlElement="True" IsEnabled="True" IsKeyboardFocusable="False" IsOffscreen="False" IsPassword="False" IsRequiredForForm="False" ItemStatus="" ItemType="" LocalizedControlType="text" Name="Click Me" Orientation="None" ProcessId="26984" RuntimeId="7.26984.38376892" x="67" y="77" width="46" height="16" />
</Button>
</Window>
Are you able to run the UnitTest1.cs -> TestMethod1() without any implications ?
Yes - it passed.
The xml you are showing, is that collected from the DumpXmlPageSource() called from the TestMethod1()?
Yes - that's correct.
Can you comment on which Windows 10 build/version you're using (Start > Run > enter "Winver")?
We're running on 1803 (Build 17134.285).
Thanks for the reply @hassanuz
Ive tested on two environments without success :/ Windows versions are 17134.320 for both machines.
I will try running on some other colleagues machines and notify if I get any other results.
I have the same problem, used from Robot framework.
AppiumLibrary is not able to find element on win32 application on a dropdown/context menu.
I try to automate few steps on microSIP soft client, and a dropdown menu will open dynamically when RF clicks on the menu button.
Also element was not visible in source that was retrieved by Get Source keyword.
Text 'xpath=//*[@Name="Edit Account"]' did not appear in 5 seconds
Could you advise, when this bug could be fixed?
Hi @buscseik,
For your scenario, context menus are not parented to the application window, but to the desktop. In order to find the context menu, you will need to access the root desktop and find the menu.
You can find details on https://github.com/Microsoft/WinAppDriver/wiki/Frequently-Asked-Questions#when-and-how-to-create-a-desktop-session
As you can see in the image, the context menu is actually a peer of the MicroSIP window.

Hi @khouzam
Thank you for the help. You have right.
I am just wondering if is there any function that helps to retrieve the window handler based on title or class name in WinAppDriver?
Hi, I am having the same issue. It seems like context menu has always ClassName #32768, but even if I try to find it via Desktop session and XPath, it won't find it and it timeouts. Trying it on the latest Insider build and Selenium 3.14.0.
DesktopSession.FindElementByXPath("//*[@ClassName='#32768']");
It is not even timing issue as I tried to put some sleep before trying to look for that menu.
Any other ideas?
