考虑我下面的Appium测试代码(WinappDriver)。
据我所知,这两个定位器应该是等价的。然而,第二次失败是因为找不到元素。
难道XPath没有给出结果的原因吗?
// does not throw an exception
var header = App.Session.FindElementByName("NIEUW FORMULIER");
// throws an exception
header = App.Session.FindElementByXPath("//*[@Name='NIEUW FORMULIER']");这是inspect.exe视图:

<?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 & 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\" />发布于 2017-07-11 07:24:15
找到了一半的解决方案,记录问题:XPath可以在另一个元素的上下文中使用它,而不是在从驱动程序级别进行搜索时工作。
// Does not work
var button = CalculatorSession.FindElementsByXPath(".//Button");
// Works
var w = CalculatorSession.FindElementByClassName("Windows.UI.Core.CoreWindow");
var plus = w.FindElementsByXPath(".//*[@Name=\"Plus\"]");发布于 2017-06-30 14:12:30
这可能是xpath是类、名称和css的组合。因此,要获得一个xpath,就需要与所有上述finder类型合并。但是当提到名字的时候,它是独一无二的。而且,如果分析web页面加载模式,您将看到Name将比xpath加载得更快。因此,appium获得名称而不是Xpath是简单而快捷的。
发布于 2017-06-30 14:20:02
等价物是//*[@name='NIEUW FORMULIER']。
尽管如此,这并不是一个直接的等价物,因为也可能涉及名称空间--很少涉及这种情况,但值得一提的是。
https://sqa.stackexchange.com/questions/28163
复制相似问题