Appium 难道只能使用 xpath?ID 不可以用吗?

sam li · 2014年03月31日 · 最后由 sam li 回复于 2014年04月21日 · 1783 次阅读

android 4.3 操作系统点击 id 报错?

1,
capabilities.setCapability("device", "Android"); //Selendroid
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.3");
capabilities.setCapability(CapabilityType.PLATFORM, "WEBVIEW");
capabilities.setCapability("app", app.getAbsolutePath());

2,
driver.findElement(By.id("buttonLogin")).click(); 就报错了,为何?(Selendroid 是安卓 Android devices < 4.2, 才使用的:https://github.com/appium/appium/blob/master/docs/running-tests.md#preparing-your-app-for-test-android

3,报错如下:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 659 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'
System info: host: 'sam-PC', ip: '192.168.0.211', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Session ID: fe23e444-93cc-419f-8b44-9e58df33305c
Driver info: com.myappiumtest.app.myapp.AndroidContactsTest$SwipeableWebDriver
Capabilities [{app=C:\Users\Administrator\shanliao_03_00_11_sign.apk, platform=UNIX, databaseEnabled=false, javascriptEnabled=true, browserName=, app-package=com.mengdi.shanliao, webStorageEnabled=false, device=Android, app-activity=.loginregister.LoginMainActivity, desired={platform=WEBVIEW, app=C:\Users\Administrator\shanliao_03_00_11_sign.apk, browserName=, app-package=com.mengdi.shanliao, device=Android, app-activity=.loginregister.LoginMainActivity, version=4.3}, warnings={}, takesScreenshot=true, version=4.3}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:389)
at org.openqa.selenium.By$ById.findElement(By.java:214)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
at com.myappiumtest.app.myapp.AndroidContactsTest.shanliao(AndroidContactsTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

共收到 5 条回复 时间 点赞

这个 ID 你是怎么得到的,似乎不是用 hierarchy viewer 找到的那个,是 resource-id

用 uiautomatorviewer 去定位下对应的 id 吧. find_by_name 是支持 resourceid 的, 不过只在高版本里面支持

#2 楼 @seveniruby 我用 4.3 手机是支持 resource-id 的。发现: 4.3 以下的手机都不支持 resource-id 真坑啊 为什么不支持所有系统? 像 robotium 只要是 hierarchy viewer 能找到的 id 都可以用的

#3 楼 @lfcoolhappy 这个跟 uiautomator 有关 使用 selendroid 模式也可以支持 4.3 以下的所有手机的
appium 是支持两种模式的

#4 楼 @seveniruby 选择 selendroid 模式后 resource-id 会报错的(4.2)

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