H5 元素定位


app開發三種模式:native    h5   hybird(混合)

針對不同場景使用不同的定位方式,android原生控件,web是b\s架構,兩者在不同環境下運行,此需要進行上下文context切換,然后對h5頁面元素定位操作

context

簡介

在開發中說的上下文,android源碼中的注釋是:關於應用程序環境的全局信息的接口,這是一個抽象類,其實現由android系統提供,它允許訪問特定於應用程序的資源和類,以及對應用程序級操作的調用,如啟動活動、廣播和接收意圖等

android四大組件:Activity /Service/  BroadCast Recevicer/ Content provider。

通俗理解context為當前對象在程序中所處的環境,原生界面屬於一個activity,也就是android的界面,但是訪問內嵌的網頁是屬於網頁環境,兩者處於不同的環境

webview

是android系統提供的能顯示網頁的系統控件,它是一個特殊的view,同時它是一個viewgroup,可以有很多子view,android 4.4以上用的是google chromium作為系統webview的內核

h5環境搭建

pc谷歌瀏覽器和手機谷歌瀏覽器版本號一致

chromedriver與chrom瀏覽器版本號對應    http://npm.taobao.org/mirrors/chromedriver/或者http://chromedriver.storage.googleapis.com/index.html 或者https://sites.google.com/a/chromium.org/chromedriver/downloads   下載地址      https://blog.csdn.net/weixin_42244754/article/details/81541894                     https://blog.csdn.net/BinGISer/article/details/88559532       chromdriver與chrom對應表

下載chromedriver存放路徑appium安裝路徑:C:\Users\XXXX\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win

操作步驟:

1、手機與電腦連接,開啟usb調試模式(設備系統5.0以上)

2、電腦端和移動端必須安裝chrome瀏覽器

3、app webdirver開啟debug模式

4、在電腦端輸入chrome://inspect/#drevices,進入調試模式(或者復制h5的鏈接,通過谷歌瀏覽器打開)

5、執行測試腳本

webview調試模式檢查是否開啟

1、在app內打開h5頁面,在chrome://inspect/#devices 地址中,檢查是否顯示對應的webview,如沒有,未開啟調試模式

2、在自動化腳本中,進入對應用的h5頁面,打印出當前的context,如果 一直顯示antive app,則說明webview未開啟

開啟方式

在app中配置如下代碼,在webview類中調用靜態方法setwebcontentxdebuggingenabled,如果app調試模式打不開,可以通過抓包工具,把鏈接復制到手機谷歌瀏覽器,pc瀏覽器打開后,也可以定位頁面元素

代碼塊:app-src-main-java-包名-abc-app.java

if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}

 

需求分析

1\先進入到h5頁面,然后切換到context,再進行相關元素定位操作

2\context切換:可以通過context()方法獲取到頁面的所有context,然后切換到h5頁面的context

3\在h5頁面進行元素定位操作

獲取方法:   context = driver.contexts      print(context)

打印結果:   

代碼實現:

from   selenium.webdriver.supper.ui improt WebDriverWait     顯示等待

 

def testfone(self):

self.driver.find_element(*self.btn).click()
#等待頁面元素加載
WebDriverWait(driver,8).until(lambda x:x.find_element_by_id("com.wondershare.drfone:id/btnRecoverData"))
logging.info('等待next按鈕加載,顯示點擊next')
self.driver.find_element_by_id("com.wondershare.drfone:id/btnRecoverData").click()
# 等待webview頁面加載
WebDriverWait(driver,8).until(lambda x:x.find_element_by_class_name("android.webkit.WebView"))
#打印上下文環境
contexts = self.driver.contexts
print(contexts)
logging.info('開始切換環境h5')
self.driver.switch_to.context('WEBVIEW_com.wondershare.drfone')
print(contexts)
logging.info('點擊輸入郵件地址並添加郵件')
self.driver.find_element_by_id('email').send_keys('lilei@qq.com')
logging.info('點擊發送')
self.driver.find_element_by_class_name('btu_send').click()
#切換回原生
logging.info('切換原生環境')
self.driver.switch_to.context('NATIVE_APP')
logging.info('點擊返回按鈕')
self.driver.find_element_by_class_name('android.widget.ImageButton').click()
logging.info('測試完成')

 

 

pc端

find_element_by_id

find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

 

 

 x5內核

x5內核是騰訊公司基本優秀開源webkit深度優化的瀏覽器渲染引擎,2014開放給app開發者,所以現在很多app都是使用x5內實現hybird混合應用

開啟x5調試

http://debugmm.qq.com/?forcex5=true

http://debugx5.qq.com

找到web頁面所屬進程

adb shell dumpsys activity top | findstr ACTIVITY

adb shell ps 進程號

啟動會話參數

  1. 支持X5內核應用自動化配置
  2.  desiredCapabilities.setCapability( "recreateChromeDriverSessions", true);

定制化啟動webview

desired_caps['chromeOptions'] = {'androidProcess':'進程名'}

 

 

 

 

 webview 三大類: 原生webview     x5內核(常用)        crosswalk

 

一、與app的開發確認瀏覽器用的是什么版本

如果用原生webview,通過下面的命令查看版本號,然后通過瀏覽器會顯示版本號,根據版本號下載相應的chromedriver,如果沒有就下載相近的

 通過cmd查詢手機webdriver的版本:adb shell am start -a android.intent.action.VIEW -d  https://liulanmi.com/labs/core.html

2、如果x5內核,訪問:chrome://inspect/#devices   查看版本號,下載相應的chromedriver

 備注:android安裝方式,一定要把安裝包放到文件管理里面安裝,如果用adb install 安裝的話,這個會不顯示

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

h5內核

 selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted    selenium.common.exceptions.InvalidSelectorException:消息:選擇器無效:不允許使用復合類名

 源碼:

 改成:self.driver.find_element_by_class_name("btn_send").click()  可以 網上找的還有其他2種不可行

 

 selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. Original error: unknown error: unable to discover open pagesselenium.common.exceptions.WebDriverException:消息:處理命令時發生未知服務器端錯誤。原始錯誤:無法啟動Chromedriver會話:處理命令時發生未知服務器端錯誤。原始錯誤:未知錯誤:無法發現打開的頁

 

 

selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. Original error: unknown error: unable to discover open pagesselenium.common.exceptions.WebDriverException:消息:處理命令時發生未知服務器端錯誤。原始錯誤:無法啟動Chromedriver會話:處理命令時發生未知服務器端錯誤。原始錯誤:未知錯誤:無法發現打開的頁

 

 

selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Failed to start Chromedriver session: A new session could not be created. Details: [object Object] session not created: This version of ChromeDriver only supports Chrome version 76

selenium.common.exceptions.WebDriverException:消息:處理命令時發生未知服務器端錯誤。原始錯誤:無法啟動Chromedriver會話:無法創建新會話。詳細信息:[對象]未創建會話:此版本的ChromeDriver僅支持Chrome版本76

因為chromedriver版本不對,需要通過chrome://inspect/#devices   查看版本號,下載相應的chromedriver

 

 

原生切換后H5 頁面滑動

以元素為起點向下滑動,實現下拉操作

  • scroll_from_element(on_element xoffset yoffset)
    • on_element:開始元素滾動。
    • xoffset:X偏移量。
    • yoffset:Y偏移量。
  • 注意:

      向下滑動為負數,向上滑動為正數

  • from selenium.webdriver.common.touch_actions import TouchActions      導入模塊
  • button = self.driver.find_element_by_xpath('//*[@id="isApp"]/div[2]')   button 元素定位到一個list,如果定位到item一直不滑動
    Action = TouchActions(driver)
    Action.scroll_from_element(button,-7000,0).perform()


免責聲明!

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



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