Appium python 版本的 webdriver 在 Android 平台下点击事件无效,求指导~

vv · 2015年11月08日 · 最后由 Stepthen 回复于 2017年05月11日 · 1770 次阅读

基本信息

环境:Android+python+appium+w8
版本:python-2.7、appium-1.4.0.0

实现功能

Android 内的 chrome 浏览器打开被测 web,实现点击 [注册] BTN 的功能,并进行跳转

代码

#coding=utf-8
from appium import webdriver
import time

def ptest():
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '4.4.4'
    desired_caps['deviceName'] = 'MI 3'
    desired_caps['browserName'] = 'chrome'
    desired_caps['noReset'] = 'true'
    dr = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    url = ''
    dr.get(url)
    time.sleep(5)
    button = dr.find_element_by_class_name('register')
    button.click()
    dr.quit()
    return

问题

python 版本的 webdriver 在 Android 平台下点击事件失效
执行 button.click() 后,请求成功,但是 appium 返回的值始终 value 都是空
页面按钮是 js 实现的,改用下面这句也不行,但按钮绝对找到了,执行 js 的浅出效果可以生效了,就是不能 click 跳转页面

dr.execute_script('$(arguments[0]).click()', button)

LOG

担心丢失信息,我把所有 log 都贴了上来,其中标黑的部分返回值为空

Checking if an update is available
Update available to new version 1.4.13.1
Starting Node Server
warn: Appium support for versions of node < 0.12 has been deprecated and will be removed in a future version. Please upgrade!
info: Welcome to Appium v1.4.0 (REV 8f63e2f91ef7907aed8bda763f4e5ca08e86970a)
info: Appium REST http interface listener started on 127.0.0.1:4723
info: [debug] Non-default server args: {"address":"127.0.0.1","logNoColors":true,"platformName":"Android","platformVersion":"18","automationName":"Appium"}
info: Console LogLevel: debug
info: --> POST /wd/hub/session {"desiredCapabilities":{"platformVersion":"4.4.4","deviceName":"MI 3","browserName":"chrome","platformName":"Android","noReset":"true"}}
info: Client User-Agent string: Python-urllib/2.7
warn: Converting cap noReset from string to boolean. This might cause unexpected behavior.
info: Set mode: Proxying straight through to Chromedriver
info: [debug] Looks like we want chrome on android
info: [debug] Creating new appium session b4e85ce9-2c2a-44cf-8a92-f64d10246acf
info: [debug] Checking whether adb is present
info: [debug] Using adb from C:\Program Files\android-sdk-windows\platform-tools\adb.exe
info: [debug] Using fast reset? false
info: [debug] Preparing device for session
info: [debug] Not checking whether app is present since we are assuming it's already on the device
info: Retrieving device
info: [debug] Trying to find a connected android device
info: [debug] Getting connected devices...
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" devices
info: [debug] 1 device(s) connected
info: Found device 253254d5
info: [debug] Setting device id to 253254d5
info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 wait-for-device
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "echo 'ready'"
info: [debug] Starting logcat capture
info: [debug] Set chromedriver binary as: C:\Program Files (x86)\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
info: Set chromedriver binary as: C:\Program Files (x86)\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
info: [debug] Pushing unlock helper app to device...
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 install "C:\Program Files (x86)\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "dumpsys window"
info: Unlocking screen
info: [debug] Screen is locked, trying to unlock
info: [debug] Getting device API level
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 19
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n io.appium.unlock/.Unlock"
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "dumpsys window"
info: [debug] Screen is locked, trying to unlock
info: [debug] Getting device API level
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 19
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n io.appium.unlock/.Unlock"
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "dumpsys window"
info: [debug] Screen is unlocked, continuing
info: [debug] Forwarding system:4724 to device:4724
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 forward tcp:4724 tcp:4724
info: [debug] Pushing appium bootstrap to device...
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 push "C:\Program Files (x86)\Appium\node_modules\appium\build\android_bootstrap\AppiumBootstrap.jar" /data/local/tmp/
info: Starting App
info: [debug] Attempting to kill all 'uiautomator' processes
info: [debug] Getting all processes with 'uiautomator'
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "ps 'uiautomator'"
info: [debug] No matching processes found
info: [debug] Running bootstrap
info: [debug] spawning: C:\Program Files\android-sdk-windows\platform-tools\adb.exe -s 253254d5 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.android.chrome -e disableAndroidWatchers false
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
info: [debug] [BOOTSTRAP] [debug] Loading json...
info: [debug] Pushing command to appium work queue: ["getDataDir",{}]
info: [debug] [BOOTSTRAP] [debug] json loading complete.
info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
info: [debug] [BOOTSTRAP] [debug] Client connected
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDataDir","params":{}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: getDataDir
info: [debug] dataDir set to: /data/local/tmp
info: Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (netstat -nao ^| findstr /R /C:"9515 ") do (FOR /F "usebackq" %b in (TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe) do (IF NOT %b=="" TASKKILL /F /PID %a))
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"\/data\/local\/tmp","status":0}
info: No old chromedrivers seemed to exist
info: Spawning chromedriver with: C:\Program Files (x86)\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe --url-base=wd/hub --port=9515
info: [CHROMEDRIVER STDOUT] Starting ChromeDriver 2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3) on port 9515
Only local connections are allowed.
info: Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
info: Got response with status 200: {"sessionId":"","status":0,"value":{"build":{"version":"alpha"},"os":{"arch":"x86_64","name":"Windows NT","version":"6.3"}}}
info: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.android.chrome","androidDeviceSerial":"253254d5"}}}
info: Got response with status 200: {"sessionId":"de7dd879fcd4c51ea8b9653da996b15d","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{},"cssSelect...
info: [debug] Overriding session id with "de7dd879fcd4c51ea8b9653da996b15d"
info: [debug] Device launched! Ready for commands
info: [debug] Setting command timeout to the default of 60 secs
info: [debug] Appium session started with sessionId de7dd879fcd4c51ea8b9653da996b15d
info: <-- POST /wd/hub/session 303 29651.625 ms - 70
info: --> GET /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d {}
info: Proxying [GET /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d] to [GET http://127.0.0.1:9515/wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d] with body: {}
info: Got response with status 200: {"sessionId":"de7dd879fcd4c51ea8b9653da996b15d","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{},"cssSelect...
info: <-- GET /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d 200 9.757 ms - 506
info: --> POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/url {"url":"http://as-stage.wecash.net/wap_v4,sessionId:de7dd879fcd4c51ea8b9653da996b15d"}
info: Proxying [POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/url] to [POST http://127.0.0.1:9515/wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/url] with body: {"url":"http://as-stage.wecash.net/wap_v4,sessionId:de7dd879fcd4c51ea8b9653da996b15d"}
info: Got response with status 200: {"sessionId":"de7dd879fcd4c51ea8b9653da996b15d","status":0,"value":null}
info: <-- POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/url 200 2155.140 ms - 72
info: --> POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/element {"using":"class name","sessionId":"de7dd879fcd4c51ea8b9653da996b15d","value":"register"}
info: Proxying [POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/element] to [POST http://127.0.0.1:9515/wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/element] with body: {"using":"class name","sessionId":"de7dd879fcd4c51ea8b9653da996b15d","value":"register"}
info: Got response with status 200: {"sessionId":"de7dd879fcd4c51ea8b9653da996b15d","status":0,"value":{"ELEMENT":"0.30392428231425583-1"}}
info: <-- POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/element 200 125.408 ms - 103
info: --> POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/element/0.30392428231425583-1/click {"sessionId":"de7dd879fcd4c51ea8b9653da996b15d","id":"0.30392428231425583-1"}
info: Proxying [POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/element/0.30392428231425583-1/click] to [POST http://127.0.0.1:9515/wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/element/0.30392428231425583-1/click] with body: {"sessionId":"de7dd879fcd4c51ea8b9653da996b15d","id":"0.30392428231425583-1"}
info: Got response with status 200: {"sessionId":"de7dd879fcd4c51ea8b9653da996b15d","status":0,"value":null}
info: <-- POST /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d/element/0.30392428231425583-1/click 200 229.823 ms - 72
info: --> DELETE /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d {}
info: Shutting down appium session
info: Proxying [DELETE /] to [DELETE http://127.0.0.1:9515/wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d] with no body
info: Got response with status 200: {"sessionId":"de7dd879fcd4c51ea8b9653da996b15d","status":0,"**value":null}
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"OK, shutting down","status":0}
info: [debug] [BOOTSTRAP] [debug] Closed client connection
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
info: [debug] [UIAUTOMATOR STDOUT] Time: 16.286
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] UiAutomator shut down normally
info: [debug] executing cmd: "C:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s 253254d5 shell "am force-stop com.android.chrome"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] Cleaning up appium session
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"de7dd879fcd4c51ea8b9653da996b15d"}
info: <-- DELETE /wd/hub/session/de7dd879fcd4c51ea8b9653da996b15d 200 1590.341 ms - 72 {"status":0,"value":null,"sessionId":"de7dd879fcd4c51ea8b9653da996b15d"}
Killed Node Server.
Node Server Process Ended

PS:请各位大神指点一二,这个问题困扰了很久了,多谢 (>_<)

共收到 11 条回复 时间 点赞
  1. 没有排版
  2. 错误日志没有给出

phython 是啥。。。

vv #3 · 2015年11月11日 Author

@lihuazhang @seveniruby @monkey 已修改,麻烦帮忙确认一下是否可以了,谢谢哈

@vv_0223 首先你的页面是这样的:

(function () {
    var self = null
    var welcome = {
        init: function () {
            self = this
            self.bind()
        },
        bind: function () {
            $(document)
            .on('tap', '#register', function () {
                location.assign('register_first_step.html?v=' + new Date().getTime())
            })
            .on('tap', '#login', function () {
                location.assign('login.html?v=' + new Date().getTime())
            })
        }
    }
    welcome.init()
})()

也就是说 click 估计压根不能触发你的 tap 事件,所以肯定不能跳转。

试试看执行下面的脚本,我测试是可以的。

function triggerEventOnPage(selector, eventName, memo) {
    //casper.evaluate(function(selector, eventName, memo){
        var event;
        var element = document.querySelector(selector);

        event = document.createEvent("Event");
        event.initEvent(eventName, true, true);
        event.memo = memo || { };

        element.dispatchEvent(event);
    //}, selector, eventName, memo);
    //wait();
}
triggerEventOnPage("#register", "tap");

在移动 web 上 click、tap、touch 这几个事件是不太一样的,click 一百有几百毫秒的延迟,而非单纯的单击操作,所以浅出效果生效了,但没有跳转。

vv #6 · 2015年11月15日 Author

#5 楼 @lihuazhang
多谢,可以了,但是这样看来只要是 js 实现的,就都要用 js 脚本执行测试了,试过 webdriver 的 send_keys() 也不行,估计也是这个问题,再研究下,真实太感谢了:)

#7 楼 @vv_0223 自己研究,我已经给你思路和解决方案了。

vv #9 · 2015年11月15日 Author

#8 楼 @lihuazhang
恩,可以了,就是执行脚本的问题,多谢多谢:)

我按照你的设置打开浏览器报错:No app set; either start appium with --app or pass in an 'app' value in desired capabilities, or set androidPackage to launch pre-existing app on device
必须要设置 app 啊?

王乐新 微信 webview 的自动化技术 中提及了此贴 04月20日 18:08

用 Android Chrome 测试时最好是将 context 切换到"CHROMIUM"模式,然后用 click() 方法就可以了。
我刚才试了一下是可以的(C#):

((IContextAware)driver).Context = "CHROMIUM"; 
driver.FindElementByXPath("//*[text()='注册']").Click()
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册