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}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM