Appium appium xpath 定位问题

程橙 · 2016年09月26日 · 最后由 程橙 回复于 2016年09月28日 · 1511 次阅读

刚开始接触 appium,准备用公司的 android app 写一个简单的例子,遇到一个很奇葩的问题,求大神帮忙。。。。

同一个页面,通过 uiautomatorviewer 获取的页面元素却不一样,appium 的 log source 也只能获取到图一种的页面
图一:
图二:

共收到 15 条回复 时间 点赞

建议使用 macaca 的 app inspector,这个确实科学,好用

#2 楼 @lingcizhisheng app-inspector 貌似不支持 ios 真机吧?

有时候 UIAutomatorViewer 会把 WebView 当成 native 控件,有时候不会。没有弄明白这里的原理。
可以不要依赖 UIAutomator ,切换 Context 当 Web 测试。

#4 楼 @sanlengjingvv如何不依赖 UIAutomator,不懂,求详解

如果是混合应用,用 UIAutomator 不行的,切换 context 试试,driver.context,把结果 print 出来看看里面的 webview,然后 driver.switch_to.context('(这里输入你前面 print 出来的 webview)'),然后就可以操作 DOM 元素,你可以 print (page_resouce) 打印当前页面的元素

#6 楼 @Hoyeung 通过 Get Current Context 和 Switch To Context 这两个方法把 context 切换到了 NATIVE_APP,又通过 Log Source 获取到了页面,但是页面中只包含图一中的元素,图二中的元素完全定位不到

@amber_cheng 什么软件,我试试

差旅随行

程橙 #11 · 2016年09月28日 Author

#8 楼 @Hoyeung 跳过引导页,进入登录页面后,应该先获取该页面的所有元素,确定元素中包含公司编码才能定位到。对吧?你用 uiautomatorviewer 定位时,不要选中页面中的任何元素

程橙 #12 · 2016年09月28日 Author

#10 楼 @Hoyeung 重现图一和图二是有规律的。
1、我如果获取公司编码输入框的焦点(会有一个小键盘出现),图二出现的几率就很大。
2、我如果进入页面后,没有任何操作,直接获取该页面的所有元素,图一出现的几率就很大
3、疑问:这个跟 uiautomatorviewer(版本)有关系吗???

#12 楼 @amber_cheng 跳过引导页的时候,我用了休眠确保能定位到登陆页面,就能打印出来页面元素了

程橙 #13 · 2016年09月28日 Author

#13 楼 @Hoyeung 直接贴代码
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=4.4.2 deviceName=Android Emulator app=${CURDIR}/demoapp/chailvsuixing.apk appPackage=com.travelsky.bluesky appActivity=.UIView automationName=appium unicodeKeyboard=True
sleep 5s

# 引导页

Swipe 600 720 120 720 2000

Swipe 600 720 120 720 2000

Swipe 600 720 120 720 2000

Swipe 600 720 120 720 2000

Swipe 600 720 120 720 2000

sleep 3s

Click Element id=android:id/content

sleep 5s

${a} Get Contexts

log ${a}

Switch To Context ${a}

sleep 3s

Log Source

# 登录页面

comment 输入公司编码

Input Text xpath=//android.widget.EditText[contains(@index,0)] publish

comment 用户名称

Input Text xpath=//android.widget.EditText[contains(@index,1)] amber.cheng@citsgbt.com

comment 用户密码

Input Password xpath=//android.widget.EditText[contains(@index,2)] 123456a

Comment 点击登录按钮

Click Element xpath=//android.widget.Button[contains(@content-desc,'登录')]

sleep 5s

程橙 #15 · 2016年09月28日 Author

问题已解决,使用 6 楼 Hoyeung 的建议,切换到了 webview 模式

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