1.開啟微信debug模式
在微信瀏覽器中打開:debugx5.qq.com,在信息頁面勾選‘打開TBS內(nèi)核Inspector調(diào)試功能’

在電腦chrome瀏覽器輸入:chrome://inspect/#devices ,查看微信webview版本號

2. 代碼配置
# 微信公眾號 vivo
desired_caps_vivo_weixin= {
????'platformName': 'Android',
????'platformVersion': '8.1',
????'deviceName': 'PD1635',
????'noReset': 'True',
????'appPackage': 'com.tencent.mm',
????'appActivity': '.ui.LauncherUI',
????'automationName': "Uiautomator2",
????'newCommandTimeout': 240,
????'chromeOptions': {
????????'androidProcess': 'com.tencent.mm:tools'
? ? },
????'unicodeKeyboard': True,
????'resetKeyboard': True
}
3.操作公眾號(代碼實現(xiàn))
首先要關(guān)注微信公眾號‘心醫(yī)國際(西安)’,執(zhí)行以下代碼
driver = webdriver.Remote(command_executor =?'http://127.0.0.1:4723/wd/hub',desired_capabilities = desired_caps_vivo_weixin)
#輸入搜索內(nèi)容
driver.find_element_by_id('com.tencent.mm:id/kh').send_keys('心醫(yī)國際西安')
time.sleep(5)
#點擊搜索結(jié)果
driver.find_element_by_id('com.tencent.mm:id/q0').click()
time.sleep(5)
#點擊菜單-延安APP
driver.find_element_by_xpath('//android.widget.TextView[@text="APP項目"]').click()
time.sleep(2)
driver.find_element_by_xpath('//android.widget.TextView[@text="延安APP"]').click()
time.sleep(5)
cts = driver.contexts
print(cts)? ? ?# 打印結(jié)果:['NATIVE_APP', 'WEBVIEW_com.tencent.mm:tools'],默認(rèn)到webview下
#切換到webview
driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
print('切換成功')
#打印webview頁面html
print(driver.page_source)
4.問題處理
執(zhí)行代碼driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')報錯,可以看到是因為chrome和chromedriver版本不對應(yīng)導(dǎo)致,chrome版本由chrome://inspect/#devices 查到為66.0.3359.126,Chromedriver版本為2.46。

查看chromedriver和chrome的對比表:https://blog.csdn.net/huilan_same/article/details/51896672
Chromedriver下載地址:http://chromedriver.storage.googleapis.com/index.html
將對應(yīng)的Chromedriver下載后放在指定文件夾下:

啟動配置中增加:'chromedriverExecutableDir':'D://tools//chromedriver//2.38'
執(zhí)行代碼啟動微信公眾號,之后的webview頁面元素獲取使用selenium框架。
selenium元素定位參考文件: