文章的主要思路來(lái)自于seveniruby的Appium 微信 webview 的自動(dòng)化技術(shù),非常感謝~~
通過(guò)微信打開(kāi)debugx5.qq.com 勾選【打開(kāi)TBS內(nèi)核Inspector調(diào)試功能】 
手機(jī)連接電腦,查看是否連接成功。如下展示設(shè)備號(hào)則為連接成功
進(jìn)入任意小程序,以【X東】為例,在Chrome瀏覽器訪(fǎng)問(wèn)chrome://inspect/#devices
點(diǎn)擊inspact,即可查看頁(yè)面元素 
微信有很多的進(jìn)程,每一個(gè)小程序都運(yùn)行在不同的進(jìn)程中。 進(jìn)入【X東】后,看下當(dāng)前運(yùn)行在哪個(gè)進(jìn)程中
我們可以看到,當(dāng)前的小程序運(yùn)行在com.tencent.mm:appbrand0中,記住這個(gè)進(jìn)程,記住啦
最關(guān)鍵的一點(diǎn),需要添加androidProcess這一項(xiàng),也就是我們上面的com.tencent.mm:appbrand0 我用python寫(xiě)一個(gè)簡(jiǎn)單的demo
desired_caps = {
? ? ? ? ? ?'platformName': 'Android',
? ? ? ? ? ?'fastReset': 'false',
? ? ? ? ? ?'deviceName': 'XXXXXX',
? ? ? ? ? ?'appPackage': 'com.tencent.mm',
? ? ? ? ? ?'appActivity': '.ui.LauncherUI',
? ? ? ? ? ?'fullReset': 'false',
? ? ? ? ? ?'unicodeKeyboard': 'True',
? ? ? ? ? ?'resetKeyboard': 'True',
? ? ? ? ? ?'chromeOptions': {
? ? ? ? ? ? ? ?'androidProcess': 'com.tencent.mm:appbrand0'
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.find_element_by_name("發(fā)現(xiàn)").click()
driver.find_element_by_name("小程序").click()
driver.find_element_by_name("X東購(gòu)物").click()
driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0')
time.sleep(5)
print(driver.page_source)
我遇到過(guò)一種切換Webview失敗的情況,下面貼上appium的報(bào)錯(cuò)日志。
> info: Chromedriver: Changed state to 'stopped' > error: Chromedriver: Chromedriver exited unexpectedly with code null, signal SIGTERM > warn: Chromedriver for context WEBVIEW_com.tencent.mm:appbrand1 stopped unexpectedly > warn: Chromedriver quit unexpectedly, but it wasn't the active context, ignoring > error: Chromedriver: Error: A new session could not be created. (Original error: session not created exception > from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"32496.2","isDefault":true},"id":2,"name":"","origin":"https://servicewechat.com"} > ? (Session info: chrome=57.0.2987.132) > ? (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64)) > ? ? at JWProxy.command$ (lib/proxy.js:133:15) > ? ? at tryCatch (D:Appiumnode_modulesappiumnode_modulesappium-chromedrivernode_modulesappium-jsonwp-proxynode_modulesbabel-runtimeregeneratorruntime.js:67:40) > ? ? at GeneratorFunctionPrototype.invoke [as _invoke] (D:Appiumnode_modulesappiumnode_modulesappium-chromedrivernode_modulesappium-jsonwp-proxynode_modulesbabel-runtimeregeneratorruntime.js:315:22) > ? ? at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (D:Appiumnode_modulesappiumnode_modulesappium-chromedrivernode_modulesappium-jsonwp-proxynode_modulesbabel-runtimeregeneratorruntime.js:100:21) > ? ? at GeneratorFunctionPrototype.invoke (D:Appiumnode_modulesappiumnode_modulesappium-chromedrivernode_modulesappium-jsonwp-proxynode_modulesbabel-runtimeregeneratorruntime.js:136:37) > ? ? at bound (domain.js:284:14) > ? ? at GeneratorFunctionPrototype.runBound (domain.js:297:12) > ? ? at run (D:Appiumnode_modulesappiumnode_modulesappium-chromedrivernode_modulesappium-jsonwp-proxynode_modulesbabel-runtimenode_modulescore-jslibrarymoduleses6.promise.js:89:39) > ? ? at D:Appiumnode_modulesappiumnode_modulesappium-chromedrivernode_modulesappium-jsonwp-proxynode_modulesbabel-runtimenode_modulescore-jslibrarymoduleses6.promise.js:100:28 > ? ? at flush (D:Appiumnode_modulesappiumnode_modulesappium-chromedrivernode_modulesappium-jsonwp-proxynode_modulesbabel-runtimenode_modulescore-jslibrarymodules$.microtask.js:17:13) > ? ? at process._tickDomainCallback (node.js:381:11) > ?{ [Error: A new session could not be created. (Original error: session not created exception > from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"32496.2","isDefault":true},"id":2,"name":"","origin":"https://servicewechat.com"} > ? (Session info: chrome=57.0.2987.132) > ? (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64))] > ? status: 33, > ? value: { message: 'session not created exceptionnfrom unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData": 注意最后那一段的chrome和chromedriver的版本
查看對(duì)應(yīng)表可知,兩者版本不對(duì)應(yīng),需要替換chromedriver
將Appium的chromedriver替換為相應(yīng)版本后,即可成功切換。

? 2026. All Rights Reserved. 滬ICP備2023009024號(hào)-1