如图,需要定位到红色区域的那个按钮,然后做点击操作。通过 app-inspector 查看到这个按钮属性有 xpath,然后传递该 xpath 为参数调用 wait_for_element,报错提示无法找到。是不是这个元素的 xpath 绝对路径太长不能作为参数?如果要填相对路径作为参数应该怎么写呢?

import os
import sys
from macaca import WebDriver


class iOSPhoneClass(object):

    def __init__(self):
        self.desired_caps = {
            'platformName': 'iOS',
            'platformVersion': '10.3.1',
            'deviceName': 'iPhone',
            'reuse': 2,
            'bundleId': xxx',
            'udid': 'xxx'
            #'app': 'https://npmcdn.com/ios-app-bootstrap@latest/build/ios-app-bootstrap.zip',
        }
        self.server_url = {
            'hostname': 'localhost',
            'port': 3456
        }
        self.driver = WebDriver(self.desired_caps, self.server_url)
        self.driver.init()


    def __del__(self):
        self.driver.quit()


    def exposed_wait_for_element(self, using, value, timeout = 10000):
        return self.driver.wait_for_element(using, value, timeout)


if __name__ == '__main__':
    ios = iOSPhoneClass()
    ios.exposed_wait_for_element("xpath", "//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[3]")

macaca server 的 log

>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:46:50]: method: POST url: /wd/hub/session, jsonBody: {"desiredCapabilities":{"platformName":"iOS","platformVersion":"10.3.1","deviceName":"iPhone","reuse":2,"bundleId":"xxx","udid":"xxx"}}
>> session.js:47:10 [master] pid:1165 Creating session, sessionId: 53209609-b0f4-4c7f-8373-84f42a13c88f.
>> macaca-ios.js:235:12 [master] pid:1165 App "xxx" is already installed.
>> xctest-client.js:63:14 [master] pid:1165 project path: /usr/local/lib/node_modules/macaca-ios/node_modules/webdriveragent/WebDriverAgent/WebDriverAgent.xcodeproj
>> macaca-ios.js:150:10 [master] pid:1165 {
    "bundleId": "xxx",
    "platformName": "iOS",
    "platformVersion": "10.3.1"
}
>> macaca-ios.js:154:12 [master] pid:1165 Trying to start wda server...
>> xctest-client start with port: 8900
>> xctest-client.js:243:14 [master] pid:1165 xcode version: 8.3.2
>> WebDriverAgent version: 1.1.6
>> xctest-client.js:191:14 [master] pid:1165 2017-05-17 13:46:52.904 xcodebuild[1297:44612]  IDETestOperationsObserverDebug: Writing diagnostic log for test session to:
/Users/samsung/Library/Developer/Xcode/DerivedData/WebDriverAgent-earnspoghlsscjbxlmzvvcsmvulx/Logs/Test/7E609EEB-7CCA-4DDE-8405-B96FACFEB64B/Session-WebDriverAgentRunner-2017-05-17_134652-gGIiU7.log

>> xctest-client.js:192:14 [master] pid:1165 please check project: /usr/local/lib/node_modules/macaca-ios/node_modules/webdriveragent/WebDriverAgent/WebDriverAgent.xcodeproj
>> xctest-client.js:191:14 [master] pid:1165 2017-05-17 13:46:52.905 xcodebuild[1297:44611] [MT] IDETestOperationsObserverDebug: (13A5F45E-E2FC-4620-8BAA-8396210B9CAF) Beginning test session WebDriverAgentRunner-13A5F45E-E2FC-4620-8BAA-8396210B9CAF at 2017-05-17 13:46:52.905 with Xcode 8E2002 on target <DVTiOSDevice: 0x7f860775c9f0> {
        deviceSerialNumber:         DNQLX58JFP6K
        identifier:                 xxx
        deviceClass:                iPhone
        deviceName:                 iPhone
        deviceIdentifier:           xxx
        productVersion:             10.3.1
        buildVersion:               14E304
        deviceSoftwareVersion:      10.3.1 (14E304)
        deviceArchitecture:         arm64
        deviceTotalCapacity:        12441415680
        deviceAvailableCapacity:    11047510016
        deviceIsTransient:          NO
        ignored:                    NO
        deviceIsBusy:               NO
        deviceIsActivated:          YES
        deviceActivationState:      Activated
        isPasscodeLocked:           NO
        deviceType:                 <DVTDeviceType:0x7f86075cd4c0 Xcode.DeviceType.iPhone>
        supportedDeviceFamilies:    (
    1
)
        applications:              (null)
        provisioningProfiles:      (null)
        activityProgress:          -2
        activityTitle:             
        hasInternalSupport:        NO
        isSupportedOS:             YES
        developerDiskMountError:   (null)
(null)
    bootArgs:                  <unavailable>
        } (10.3.1 (14E304))

>> xctest-client.js:192:14 [master] pid:1165 please check project: /usr/local/lib/node_modules/macaca-ios/node_modules/webdriveragent/WebDriverAgent/WebDriverAgent.xcodeproj
>> xctest-client.js:191:14 [master] pid:1165 writeDictToFile:1278 ==== Successfully wrote Manifest cache to /var/folders/yv/724_f16x4ks_mpyhnb7wg9d80000gn/C/com.apple.DeveloperTools/All/Xcode/EmbeddedAppDeltas/bf787fd53c9e7f22ed2fab69612b6737/xxx/ManifestCache.plist

>> xctest-client.js:192:14 [master] pid:1165 please check project: /usr/local/lib/node_modules/macaca-ios/node_modules/webdriveragent/WebDriverAgent/WebDriverAgent.xcodeproj
>> proxy.js:56:14 [master] pid:1165 Proxy: /session:POST to http://127.0.0.1:8900/session:POST with body: {"desiredCapabilities":{"bundleId":"xxx","platformName":"iOS","platformVersion":"10.3.1"}}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"sessionId":"746B0EEE-EC63-4245-BBB7-ED0848313912","capabilities":{"device":"iphone","browserName":"Samsung ARTIK","sdkVersion":"10.3.1","CFBundleIdentifier":"com.samsung.onboarding.Artik...
>> responseHandler.js:47:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:10]: {"sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":0,"value":"{\"platformName\":\"iOS\",\"platformVersion\":\"10.3.1\",\"deviceName\":\"iPhone\",\"reuse\":2,\"bundleId\":\"xxx\",\"udid\":\"xxx\"}"}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:10]: method: POST url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:56:14 [master] pid:1165 Proxy: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element:POST to http://127.0.0.1:8900/session/746B0EEE-EC63-4245-BBB7-ED0848313912/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]","descrip...
>> session.js:107:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:11]: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]\",\"description\":\"unable to find an element\"}","sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":7}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:12]: method: POST url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:56:14 [master] pid:1165 Proxy: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element:POST to http://127.0.0.1:8900/session/746B0EEE-EC63-4245-BBB7-ED0848313912/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]","descrip...
>> session.js:107:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:12]: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]\",\"description\":\"unable to find an element\"}","sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":7}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:13]: method: POST url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:56:14 [master] pid:1165 Proxy: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element:POST to http://127.0.0.1:8900/session/746B0EEE-EC63-4245-BBB7-ED0848313912/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]","descrip...
>> session.js:107:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:14]: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]\",\"description\":\"unable to find an element\"}","sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":7}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:15]: method: POST url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:56:14 [master] pid:1165 Proxy: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element:POST to http://127.0.0.1:8900/session/746B0EEE-EC63-4245-BBB7-ED0848313912/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]","descrip...
>> session.js:107:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:15]: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]\",\"description\":\"unable to find an element\"}","sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":7}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:16]: method: POST url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:56:14 [master] pid:1165 Proxy: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element:POST to http://127.0.0.1:8900/session/746B0EEE-EC63-4245-BBB7-ED0848313912/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]","descrip...
>> session.js:107:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:17]: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]\",\"description\":\"unable to find an element\"}","sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":7}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:18]: method: POST url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:56:14 [master] pid:1165 Proxy: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element:POST to http://127.0.0.1:8900/session/746B0EEE-EC63-4245-BBB7-ED0848313912/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]","descrip...
>> session.js:107:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:18]: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]\",\"description\":\"unable to find an element\"}","sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":7}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:19]: method: POST url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:56:14 [master] pid:1165 Proxy: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element:POST to http://127.0.0.1:8900/session/746B0EEE-EC63-4245-BBB7-ED0848313912/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]","descrip...
>> session.js:107:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:20]: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]\",\"description\":\"unable to find an element\"}","sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":7}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:21]: method: POST url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element, jsonBody: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:56:14 [master] pid:1165 Proxy: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f/element:POST to http://127.0.0.1:8900/session/746B0EEE-EC63-4245-BBB7-ED0848313912/element:POST with body: {"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]"}
>> proxy.js:84:20 [master] pid:1165 Got response with status 200: {"value":{"using":"xpath","value":"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]","descrip...
>> session.js:107:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:21]: {"value":"{\"using\":\"xpath\",\"value\":\"//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]\",\"description\":\"unable to find an element\"}","sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":7}
>> responseHandler.js:11:12 [master] pid:1165 Recieve HTTP Request from Client[2017-05-17 13:47:21]: method: DELETE url: /wd/hub/session/53209609-b0f4-4c7f-8373-84f42a13c88f, jsonBody: {}
>> macaca-ios.js:343:10 [master] pid:1165 Stoping iOS driver...
>> xctest-client.js:279:14 [master] pid:1165 killing deviceLogProc pid: 1294
>> xctest-client.js:284:14 [master] pid:1165 killing runnerProc pid: 1297
>> xctest-client.js:290:14 [master] pid:1165 killing iproxyProc pid: 1296
>> macaca-ios.js:367:10 [master] pid:1165 iOS driver cleaned up.
>> session.js:80:12 [master] pid:1165 Delete session, sessionId: 53209609-b0f4-4c7f-8373-84f42a13c88f
>> responseHandler.js:47:14 [master] pid:1165 Send HTTP Respone to Client[2017-05-17 13:47:21]: {"sessionId":"53209609-b0f4-4c7f-8373-84f42a13c88f","status":0}
>> xctest-client.js:157:16 [master] pid:1165 deviceconsole exit with code: null, signal: SIGKILL
>> xctest-client.js:230:14 [master] pid:1165 iproxy exit with code: null, signal: SIGKILL
>> xctest-client.js:202:14 [master] pid:1165 xctest client exit with code: null, signal: SIGKILL


↙↙↙阅读原文可查看相关链接,并与作者交流