-1

Consider my below test code for Appium (WinappDriver).

As far as I know, these two locators should be equivalent. Yet the second one fails because the element cannot be found.

Is there any reason why XPath doesn't give results?

// does not throw an exception    
var header = App.Session.FindElementByName("NIEUW FORMULIER");

// throws an exception
header = App.Session.FindElementByXPath("//*[@Name='NIEUW FORMULIER']");

This is the inspect.exe view:

enter image description here

<?xml version=\"1.0\" encoding=\"utf-16\"?>
<Window AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"TitleBar\" ClassName=\"ApplicationFrameWindow\" FrameworkId=\"Win32\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"False\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"\" Name=\"Accent Mobile Forms\" Orientation=\"None\" ProcessId=\"7932\" RuntimeId=\"42.2688684\" x=\"-8\" y=\"0\" width=\"1483\" height=\"838\" CanMaximize=\"False\" CanMinimize=\"False\" IsModal=\"False\" WindowVisualState=\"Normal\" WindowInteractionState=\"Running\" IsTopmost=\"False\" CanRotate=\"False\" CanResize=\"False\" CanMove=\"False\" IsAvailable=\"True\">
    <Window AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"Windows.UI.Core.CoreWindow\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"venster\" Name=\"Accent Mobile Forms\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662\" x=\"1\" y=\"40\" width=\"1465\" height=\"789\">
        <Window AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"Popup\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"venster\" Name=\"Pop-up\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.1\" x=\"1\" y=\"773\" width=\"1465\" height=\"56\">
            <AppBar AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"ApplicationBar\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"app-balk\" Name=\"\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.67\" x=\"1\" y=\"773\" width=\"1465\" height=\"55\" ExpandCollapseState=\"Collapsed\" ToggleState=\"Off\" IsAvailable=\"True\">
                <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"AppName\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"ACCENT MOBILE FORMS\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.68\" x=\"636\" y=\"779\" width=\"196\" height=\"24\" />
                <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"AppVersion\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"ACCEPT - Versie 2017.6.29.4\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.69\" x=\"639\" y=\"803\" width=\"184\" height=\"20\" />
                <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"True\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.70\" x=\"-299\" y=\"-124\" width=\"0\" height=\"0\">
                    <HyperLink AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"Hyperlink\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"True\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"hyperlink\" Name=\"ms-windows-store://navigatetopage/?Id=LOB\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.71\" x=\"-299\" y=\"-124\" width=\"0\" height=\"0\" />
                </Text>
            </AppBar>
        </Window>
        <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"R028 - ACCENT INDUSTRY &amp; PRODUCTION ROESELARE\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.50\" x=\"14\" y=\"59\" width=\"625\" height=\"25\" />
        <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"ACTUM\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.51\" x=\"694\" y=\"45\" width=\"78\" height=\"30\" />
        <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"Klantnr: 56703 - POPERINGE\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.52\" x=\"639\" y=\"75\" width=\"188\" height=\"20\" />
        <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"ULA NAERT\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.53\" x=\"1357\" y=\"59\" width=\"96\" height=\"25\" />
        <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"Favorite\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.54\" x=\"912\" y=\"116\" width=\"30\" height=\"30\" />
        <Group AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"asbContact\" ClassName=\"AutoSuggestBox\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"groep\" Name=\"\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.55\" x=\"950\" y=\"111\" width=\"440\" height=\"41\">
            <Edit AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"TextBox\" ClassName=\"TextBox\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"True\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"bewerken\" Name=\"Zoek contact\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.72\" x=\"950\" y=\"111\" width=\"440\" height=\"41\">
                <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"Zoek contact\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.75\" x=\"966\" y=\"118\" width=\"108\" height=\"25\" />
                <Button AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"QueryButton\" ClassName=\"Button\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"knop\" Name=\"\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.76\" x=\"1347\" y=\"111\" width=\"43\" height=\"41\" />
            </Edit>
        </Group>
        <Button AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"addContactButton\" ClassName=\"Button\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"True\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"knop\" Name=\"\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.56\" x=\"1395\" y=\"111\" width=\"63\" height=\"41\" />
        <Text AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"TextBlock\" FrameworkId=\"XAML\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"False\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"False\" ItemStatus=\"\" ItemType=\"\" LocalizedControlType=\"tekst\" Name=\"NIEUW FORMULIER\" Orientation=\"None\" ProcessId=\"9440\" RuntimeId=\"42.4852662.2.57\" x=\"29\" y=\"165\" width=\"157\" height=\"24\" />
0

This might becouse of xpath is a combination of classes ,names and css. So ot need to incorporate with all the above finder types to get a one single xpath . But when it comes to Name,it is unique. And also if you analyze the web page loading pattern you will see Name will load fast than xpath. So it is easy and quickly to appium to get Name rather than Xpath.

  • This is a simple example but I need to find siblings, so FindByName is no option. – FDM Jun 30 '17 at 14:25
0

XPaths are case-sensitive.

The equivalent would be //*[@name='NIEUW FORMULIER'].

Even though, this is not a direct equivalent since there can also be namespaces involved - rarely the case, but worth mentioning.

  • Alecxe, please check my edit for a screenshot of the element properties. I've tried your suggestion but doesn't work. – FDM Jun 30 '17 at 14:30
  • @FDM interesting, I've quickly tested the idea on the google site: //*[@Name='q'] did not match any elements, //*[@name='q'] worked.. – alecxe Jun 30 '17 at 14:40
  • well I've extracted the PageSource from the driver and it says: Name=\"NIEUW FORMULIER\" - no idea why that XPath won't work... – FDM Jun 30 '17 at 14:43
  • @alexce I've added the page source up to the element in question. I don't see anything related to namespaces. But could the UTF-16 encoding be the cause? – FDM Jul 3 '17 at 5:52
  • @FDM right, no namespaces here. Not sure about the encoding - doubt this is the issue. How about this: //*[@Name='\"NIEUW FORMULIER\"'] - does it work for you? Thanks. – alecxe Jul 3 '17 at 12:22
0

Found a half solution and logged the issue: XPath works when you use it in context of another element, but not when searching from the driver level.

// Does not work 
var button = CalculatorSession.FindElementsByXPath(".//Button");

// Works 
var w = CalculatorSession.FindElementByClassName("Windows.UI.Core.CoreWindow"); 
var plus = w.FindElementsByXPath(".//*[@Name=\"Plus\"]");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.