Appium appium 运行 Android 自动化脚本 An element could not be located on the page using the given search parameters.

斯拉 · 2017年05月16日 · 最后由 斯拉 回复于 2017年05月25日 · 2475 次阅读

capabilities 配置

capabilities.setCapability("deviceName", properties.getProperty("deviceName"));
capabilities.setCapability("platformVersion", properties.getProperty("platformVersion"));
capabilities.setCapability("appPackage", properties.getProperty("appPackage"));
capabilities.setCapability("appActivity", properties.getProperty("appActivity"));
capabilities.setCapability("noReset", "true");
capabilities.setCapability("newCommandTimeout", 60000);
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4725/wd/hub"), capabilities);

执行简单的脚本

WebElement el =drive.findElementById("com.exiu.exiucarowner:id/rb0");
el.click();


[debug] [AndroidDriver] Screen unlocked successfully
[debug] [ADB] Device API level: 24
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running '/Users/exiulianmeng/Library/Android/sdk/platform-tools/adb' with args: ["-P",5037,"-s","409ecc4e","shell","am","start","-W","-n","com.exiu.exiucarowner/com.exiu.activity.CarOwnerMainActivity","-S","-a","android.intent.action.MAIN","-c","android.intent.category.LAUNCHER","-f","0x10200000"]
[Appium] New AndroidDriver session created successfully, session a69c8532-8abf-4b1b-b975-cb5383ae24bf added to master session list
[debug] [BaseDriver] Event 'newSessionStarted' logged at 1494916623269 (14:37:03 GMT+0800 (CST))
[debug] [MJSONWP] Responding to client with driver.createSession() result: {"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"appPackage":"com.exiu.exiucarowner","appActivity":"com.exiu.activity.CarOwnerMainActivity","noReset":true,"newCommandTimeout":60000,"platformVersion":"7.0","unicodeKeyboard":true,"platformName":"Android","deviceName":"409ecc4e","resetKeyboard":true},"appPackage":"com.exiu.exiucarowner","appActivity":"com.exiu.activity.CarOwnerMainActivity","noReset":true,"newCommandTimeout":60000,"platformVersion":"7.0","unicodeKeyboard":true,"platformName":"Android","deviceName":"409ecc4e","resetKeyboard":true,"deviceUDID":"409ecc4e","deviceScreenSize":"1080x1920","deviceModel":"MI 5","deviceManufacturer":"Xiaomi"}
[HTTP] <-- POST /wd/hub/session 200 116282 ms - 885 
[HTTP] --> POST /wd/hub/session/a69c8532-8abf-4b1b-b975-cb5383ae24bf/timeouts {"type":"implicit","ms":30000}
[debug] [MJSONWP] Calling AppiumDriver.timeouts() with args: ["implicit",30000,"a69c8532-8abf-4b1b-b975-cb5383ae24bf"]
[debug] [BaseDriver] Set implicit wait to 30000ms
[debug] [MJSONWP] Responding to client with driver.timeouts() result: null
[HTTP] <-- POST /wd/hub/session/a69c8532-8abf-4b1b-b975-cb5383ae24bf/timeouts 200 4 ms - 76 
[HTTP] --> POST /wd/hub/session/a69c8532-8abf-4b1b-b975-cb5383ae24bf/element {"using":"id","value":"com.exiu.exiucarowner:id/rb0"}
[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["id","com.exiu.exiucarowner:id/rb0","a69c8532-8abf-4b1b-b975-cb5383ae24bf"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 30000 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"No element found"}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [BaseDriver] Waited for 688 ms so far
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"No element found"}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [BaseDriver] Waited for 6250 ms so far
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"No element found"}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [BaseDriver] Waited for 13121 ms so far
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"No element found"}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [BaseDriver] Waited for 16936 ms so far
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.exiu.exiucarowner:id/rb0","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.exiu.exiucarowner:id/rb0' using 'ID' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.exiu.exiucarowner:id/rb0]
^C[Appium] Received SIGINT - shutting down


可是启动 app 后 在页面上手动点击一下页面,之后脚本可以顺利执行找到控件

有没有 遇到相同问题的 ?给个解决方式。

共收到 6 条回复 时间 点赞

自顶

Failed to locate element. Clearing Accessibility cache and retrying.,已经说明是 com.exiu.exiucarowner:id/rb0 没找到。你用手动点击是不对的,你截图给我看下。

可是启动 app 后 在页面上手动点击一下页面,之后脚本可以顺利执行找到控件。
关于你说的这个,我怀疑你是有 2 层,需要先在第一层找到 ID,点击确认,再进入里面,找到 com.exiu.exiucarowner:id/rb0。
第一层要加:
el = self.driver.find_element_by_id("XX")
el.click()

大海 回复

除了点击一下可以继续运行脚本外 ,还可以从登录界面,输入账号密码后,可以找到那个元素 很奇怪的是 我用模拟器是可以的不用点击 可以直接找到对象 用真机 就会一直报上文说的问题

斯拉 回复

你有截图吗

大海 回复

你说的 是我们产品的截图还是 什么截图?

斯拉 关闭了讨论 05月25日 15:59
斯拉 重新开启了讨论 05月25日 15:59

最终发现是 手机 android 7.0 系统的问题 关闭帖子

斯拉 关闭了讨论 05月25日 15:59
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册