我打算使用appium操控微信小程序,只要能夠獲取到小程序的頁面元素就算成功。下面都是我遇到的問題。
打不開啟動頁面
以下是我的appium的配置參數(shù)和代碼:
desired_caps = {
'platformName': 'Android',
'platformVersion': '10',
'automationName': 'uiautomator2',
'deviceName': 'E3LBB20402214821',
'appPackage': 'com.tencent.mm',
'appActivity': '.ui.LauncherUI',
'noReset': True,
'chromedriverExecutable': 'D://selenium//86.0.4240.22//chromedriver.exe',
'shouldTerminateApp':True,
'showChromedriverLog': True,
}
# 指定Appium Server
server = 'http://127.0.0.1:4723'
# 新建一個driver
options = AppiumOptions()
options.load_capabilities(desired_caps)
driver = webdriver.Remote(server, options=options)
print("正在打開微信呢...")
driver.implicitly_wait(5)
driver.find_element(AppiumBy.XPATH, '//*[@text="通訊錄"]')
print("打開微信成功...")
啟動appium:
appium -g C:\Users\resus\Desktop\a.txt
C:\Users\resus\Desktop\a.txt 是日志的目錄。
現(xiàn)象:
微信沒有打開,直接就定位通訊錄。
日志:
查找問題:
他說微信已經(jīng)啟動了,我根本沒啟動啊。按照他的提示,設(shè)置一個參數(shù):
'forceAppLaunch': True
這樣就可以打開微信了。
driver的context只有NATIVE_APP
代碼:
size = driver.get_window_size()
driver.swipe(size['width'] * 0.5, size['height'] * 0.4, size['width'] * 0.5, size['height'] * 0.9)
driver.find_element(AppiumBy.XPATH, '//*[@text="球場預(yù)定"]')
print("driver context", driver.contexts)
print(driver.current_context)
# 打開小程序
driver.find_element(AppiumBy.XPATH,
'//*[@content-desc="球場預(yù)定,"]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]').click()
print("driver context", driver.contexts)
print(driver.current_context)
time.sleep(10)
print("driver context", driver.contexts)
print(driver.current_context)
小程序是運行在谷歌瀏覽器里面的,是一個進程,這種也叫webview。你要獲取里面的元素,得把上下文從微信遷到小程序。
現(xiàn)象:
我打印的結(jié)果:
driver context ['NATIVE_APP']
NATIVE_APP
driver context ['NATIVE_APP']
NATIVE_APP
driver context ['NATIVE_APP']
NATIVE_APP
Process finished with exit code 0
這樣就沒有辦法操作小程序。
日志:
根本就沒有一個webview。
我這里的問題是:沒有打開debug模式。
解決:
在微信app中打開http://debugxweb.qq.com/?inspector=true,開啟debug模式。
如何檢驗是否開啟?
在谷歌瀏覽器中輸入:chrome://inspect/#devices。在微信上打開一個小程序,如果瀏覽器能夠檢測到,那就說就說明微信已經(jīng)開啟了debug模式。
再次運行。
此時就有很多webdriver的context打印出來:
driver context ['NATIVE_APP', 'WEBVIEW_com.tencent.mm:appbrand2', 'WEBVIEW_com.tencent.mm:appbrand0', 'WEBVIEW_com.tencent.mm']
我們切換到小程序的進程,就可以拿到它的頁面源碼了:
driver.switch_to.context("WEBVIEW_com.tencent.mm:appbrand0")
print("page source:", driver.page_source)
小程序上元素找不到
如果page_source打印出來沒有問題,但是依舊定位不了元素,可能是以下原因:文章來源:http://www.zghlxwxcb.cn/news/detail-735226.html
- 等我們進入到小程序的上下文了,元素定位的時候,有件事情要注意,就是要用selenium的XPATH來定位,不要用appium的:
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH,'xxxxxxxxxx')
把webview想成是PC的頁面就行。文章來源地址http://www.zghlxwxcb.cn/news/detail-735226.html
- 隱式等待打開,這個確保找不到元素是其他原因:
driver.implicitly_wait(30)
- 如果還是找不到元素,那可能是window不對。打印一下窗口有幾個,每個窗口都去試一下。
print("window_handles:", driver.window_handles)
for window in driver.window_handles:
try:
driver.switch_to.window(window)
print("current window:", driver.current_window_handle)
print("current url:", driver.current_url)
print(driver.find_element(By.XPATH, '//*[@id="fb-main"]/wx-view/wx-view[1]/wx-view[2]/wx-fb-common/wx-fb-base-button/wx-view/wx-van-button/wx-button/wx-view').text)
except Exception as e:
print(e)
到了這里,關(guān)于appium操控微信小程序的坑的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!