Macaca 关于 Macaca 测试 iOS 时使用 xpath 查找控件的问题

Michael_Wang · 2017年01月05日 · 最后由 Michael_Wang 回复于 2017年04月17日 · 2122 次阅读

问题

测试 iOS 时, 使用 xpath 查找控件, xpath 中使用属性定位不到控件.
Android 系统测试时, 可以使用.
比如:

driver.element_by_xpath("//XCUIElementTypeButton[@name='好价']")

相关 log

macaca server --verbose
>> responseHandler.js:11:12 [master] pid:47529 Recieve HTTP Request from Client: method: POST url: /wd/hub/session/594a667f-959a-48b9-96ef-feb5bf5e7481/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeButton[@name='好价']"}
>> proxy.js:56:14 [master] pid:47529 Proxy: /wd/hub/session/594a667f-959a-48b9-96ef-feb5bf5e7481/element:POST to http://172.16.24.87:8900/session/DA7E2B07-D47A-4C63-85F0-79D3EDBFC76C/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeButton[@name='好价']"}
>> proxy.js:84:20 [master] pid:47529 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeButton[@name='好价']","description":"unable to find an element"},"sessionId":"594a667f-959a-48b9-96ef-feb5bf5e7481","status":7}
>> session.js:107:14 [master] pid:47529 Send HTTP Respone to Client: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeButton[@name='好价']\",\"description\":\"unable to find an element\"}","sessionId":"594a667f-959a-48b9-96ef-feb5bf5e7481","status":7}
Python log
----> 1 driver.element_by_xpath("//XCUIElementTypeButton[@name='好价']")

/Users/smzdm/python_env/macaca/lib/python2.7/site-packages/macaca/util.pyc in find_element(self, value)
     75
     76         def find_element(self, value):
---> 77             return self.element(using.value, value)
     78
     79         find_element.__name__ = find_element_name

/Users/smzdm/python_env/macaca/lib/python2.7/site-packages/macaca/webdriver.pyc in element(self, using, value)
    812         return self._execute(Command.FIND_ELEMENT, {
    813             'using': using,
--> 814             'value': value
    815         })
    816

/Users/smzdm/python_env/macaca/lib/python2.7/site-packages/macaca/webdriver.pyc in _execute(self, command, data, unpack)
     66         res = self.remote_invoker.execute(command, data)
     67         ret = WebDriverResult.from_object(res)
---> 68         ret.raise_for_status()
     69         ret.value = self._unwrap_el(ret.value)
     70         if not unpack:

/Users/smzdm/python_env/macaca/lib/python2.7/site-packages/macaca/webdriverresult.pyc in raise_for_status(self)
     52             stacktrace = self.value.get('stacktrace', None)
     53
---> 54         raise WebDriverException(error, message, screen, stacktrace)

WebDriverException:
Error: WebDriverError.NO_SUCH_ELEMENT
Message: None

app-inspector 截图

系统环境

Mac OS: 10.12.1
wd: 0.1.5
Macaca: 1.1.21

➜  ~ macaca doctor

  macaca-doctor version: 1.0.28

  Node.js checklist:

  node env: /usr/local/bin/node
  node version: v5.0.0

  iOS checklist:

  Xcode is installed at: `/Applications/Xcode.app/Contents/Developer`
  Xcode Command Line Tools is ready, version: 2345.1.
  iproxy[usbmuxd] is installed at: `/usr/local/bin/iproxy`
  ios_webkit_debug_proxy is installed at: `/usr/local/bin/ios_webkit_debug_proxy`

  Android checklist:

  JAVA version is `1.8.0_20`
  JAVA_HOME is set to `/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home`
  ANDROID_HOME is set to `/Users/smzdm/Documents/01_Android/adt-bundle-mac-x86_64-20140702/sdk`
  Platforms is set to `/Users/smzdm/Documents/01_Android/adt-bundle-mac-x86_64-20140702/sdk/platforms/android-24`
  Android tools is set to `/Users/smzdm/Documents/01_Android/adt-bundle-mac-x86_64-20140702/sdk/tools/android`
  ADB tool is set to `/Users/smzdm/Documents/01_Android/adt-bundle-mac-x86_64-20140702/sdk/platform-tools/adb`

  Installed driver list:

  android: 1.1.15
  ios: 1.0.53
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 8 条回复 时间 点赞

这直接用 name 就好了,还用 xpath 干嘛

用这两个都找不到

element_by_name("好价")
element_by_id("好价")
``

#2 楼 @michael_wang 你这元素的 id 是空的,肯定不能用 id 方式,另外查找元素前请确保已经到达对应页面,找元素前加个等待试试

#3 楼 @adfghzhang 我明白 id 是不行的. 确认已经到达当前页面.

#4 楼 @michael_wang 有可能是你写的 xpath 确实找不到对应的元素,可以看看是不是表达式有问题,你可以复制 inspector 提供的绝对路径 xpath 来使用

#5 楼 @adfghzhang 绝对路径是没问题的.
但是, 在 app 中进行一些操作后, 同一个控件的绝对路径可能会发生变化, 所以想通过这种带有属性的 xpath 进行查找

Michael_Wang 回复

试试这个:
elements_by_class_name("XCUIElementTypeButton")[1].click()

山姆大叔 回复

好的,谢谢

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册