webview元素定位

genymotion 模拟器:android  5.0.0 

python 2.7

appium 1.4.16.1

1、app原生元素定位(常用)

driver.find_element_by_id(“resourse-id”)

driver.find_element_by_name(“text”)

driver.find_element_class_name(“class”)

driver._find_element_by_accessibility_id("content-desc")

2、android+python+webview页面的定位

问题1):driver.contexts 的结果只有NATIVE_APP,无WEBVIEW

需要开发人员在对应的activity下增加代码,重新打包,开启webview远程调试的开关,且只针对android4.4及以上有用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
     this.appView.setWebContentsDebuggingEnabled(true);
 }

问题2):获取webview的名字

contexts = .driver.contexts
for context in contexts:
    print context

问题3):切换到webview模式

driver.switch_to.context("WEBVIEW ")

问题4):切换回原生NATIVE_APP

driver.switch_to().context("NATIVE_APP")

问题5):webview的定位方式

1、手机需要安装谷歌浏览器,并打开app的网址;

2、在chrome中输入chrome://inspect/#devices,此时打开app页面

3、如果点击inspect为空白窗口,在hosts文件中添加如下地址:

61.91.161.217 chrome-devtools-frontend.appspot.com

61.91.161.217 chrometophone.appspot.com

4、然后cmd中输入命令:ipconfig /flushdns   刷新dns配置,再打开inspect

5、点击放大镜符号可以定位元素

3、此时可获取到webview的url,直接在chrome打开也可进行元素定位

1 @{contexts}    Get Contexts
2     # Log Many    @{contexts}
3     # ${cur_context}    Get Current Context
4     # log    ${cur_context}
5     # log    开始切换
6     # AppiumLibrary.Switch To Context       NATIVE_APP
7     # log    切换成功
8     # ${cur_context}    Get Current Context
9     # log    ${cur_context}
原文地址:https://www.cnblogs.com/feifei-cyj/p/8478116.html