進行調試,打開X5:
http://debugmm.qq.com/?forcex5=true http://debugx5.qq.com http://debugtbs.qq.com
一般前兩個就可以了。
微信打開:http://debugx5.qq.com,打開頁面,按圖中勾選。
=====================================================
然后再谷歌瀏覽器輸入:chrome://inspect/devices#devices,
打開頁面可能會報:
提示Pending authentication:please accept debugging session on the device
參考解決辦法:《pc調試微信h5頁面提示Pending authentication:please accept debugging session on the device的解決方法》
手機的開發者模式關掉再打開(我是用的這個方法,因為WEB自動化測試谷歌瀏覽器輕易我不升級)
=====================================================
但是上面這塊會發現點擊【inspect】會出現404或者空白頁面
解決辦法:除非翻牆或者用Webview離線包(淘寶買的定制包,只能自己使用)
用的這個程序,導入定制包就可以了
========================================================
后來發現查看小程序頁面還有另一種辦法:
右下角多了一個調試工具
點擊獲取頁面上的元素
可以定位到【我要登錄】按鈕的頁面元素:
輸入下面,查看小程序信息:
adb shell dumpsys activity top | grep ACTIVITY
adb shell ps 4527
查看小程序運行在哪里:
com.tencent.mm:appbrand0,是必須要的。
from appium import webdriver from time import sleep desired_caps = { 'platformName': 'Android', 'fastReset': 'false', 'noReset': True, 'platformVersion': '9', 'deviceName': 'b938d4a4', 'appPackage': 'com.tencent.mm', 'appActivity': '.ui.LauncherUI', 'fullReset': 'false', # 'unicodeKeyboard': 'True', # 'resetKeyboard': 'True', 'chromeOptions': { 'androidProcess': 'com.tencent.mm:appbrand0'(在這里用到) } } driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps) sleep(5) driver.find_element_by_android_uiautomator('text("微信")').click() #點擊微信Tab # 定義一個滑動屏幕的方法 def swipeDown(t): x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] x1 = int(x * 0.5) # x坐標 y1 = int(y * 0.25) # 起始y坐標 y2 = int(y * (0.25 + t)) # 終點y坐標 driver.swipe(x1, y1, x1, y2, 500) swipeDown(0.4) # 向下滑動屏幕的40%,准備從頂部進入小程序 sleep(2) driver.find_element_by_android_uiautomator('text("xxx")').click() #點擊頂部的圖標進入小程序 sleep(5) print(driver.contexts) driver.switch_to.context('WEBVIEW_com.tencent.mm:tools') sleep(5) driver.find_element_by_css_selector(".footer2").click()
但是運行后報錯:
提示當前Chromedriver版本過高2.45對應版本為70的Chrome
參考文章:《微信小程序自動化探索實踐,歡迎提意見》
輸入網址,查看Chromedriver與webview版本對應關系:
https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md
我的chrome=66.0.3359.126,所以下載2.39版本的Chromedriver
經查詢appium日志(下圖)的Chromedriver路徑為:
/Users/zhangc/.nvm/versions/node/v10.15.0/lib/node_modules/appium/node_modules/_appium-chromedriver@4.10.1@appium-chromedriver/chromedriver/mac/
把下載的替換就可以了。
但是還是報錯:定位不到元素
經過大神的文章得知,必須要進入handles
參考文章:《appium+python自動化43-微信公眾號webview操作》
需要加上下面的代碼:
handles = driver.window_handles print(handles) print(driver.current_window_handle) driver.switch_to_window(handles[1])
完整代碼如下:
from appium import webdriver from time import sleep desired_caps = { 'platformName': 'Android', 'fastReset': 'false', 'noReset': True, 'platformVersion': '9', 'deviceName': 'b938d4a4', 'appPackage': 'com.tencent.mm', 'appActivity': '.ui.LauncherUI', 'fullReset': 'false', # 'unicodeKeyboard': 'True', # 'resetKeyboard': 'True', 'chromeOptions': { 'androidProcess': 'com.tencent.mm:appbrand0' } } driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps) sleep(5) driver.find_element_by_android_uiautomator('text("微信")').click() #點擊微信Tab # 定義一個滑動屏幕的方法 def swipeDown(t): x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] x1 = int(x * 0.5) # x坐標 y1 = int(y * 0.25) # 起始y坐標 y2 = int(y * (0.25 + t)) # 終點y坐標 driver.swipe(x1, y1, x1, y2, 500) swipeDown(0.4) # 向下滑動屏幕的40%,准備從頂部進入小程序 sleep(2) driver.find_element_by_android_uiautomator('text("xxx")').click() #點擊頂部的圖標進入小程序 sleep(5) print(driver.contexts)
# 切換上下文 driver.switch_to.context('WEBVIEW_com.tencent.mm:tools') sleep(5) handles = driver.window_handles print(handles) print(driver.current_window_handle)
# 切換第二個handle driver.switch_to_window(handles[1])
# 點擊登錄按鈕 driver.find_element_by_css_selector(".footer2").click()
成功!
附錄其他參考文章:
《Android通過Chrome Inspect調試WebView的H5 App出現空白頁面的解決方法(不需要FQ)》