class Base():
driver = None
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['noReset'] = True
# desired_caps['platformVersion'] = '4.3'
# desired_caps['deviceName'] = '50a4c97e'
desired_caps['platformVersion'] = settings.platformVersion
desired_caps['deviceName'] = settings.deviceName
# desired_caps['platformVersion'] = '5.1'
# desired_caps['deviceName'] = 'A10SBNGTQJ9M'
desired_caps['appPackage'] = settings.appPackage
desired_caps['appActivity'] = settings.appActivity
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
desired_caps['chromeOptions'] = {'androidProcess': 'com.tencent.mm:appbrand0'}

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

def test(self):
print 'test'
# 打开发现页面
element_operate_instances.element_click(common_controls.wechat_find_button)
# 打开小程序页面
element_operate_instances.element_click(common_controls.small_routine_button)
# 打开小程序页面
element_operate_instances.element_click(common_controls.small_routine_me)
# 打开小程序
element_operate_instances.element_click(common_controls.self_wash_button)
# 切换到 webview
BasePage.Base.driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0')
time.sleep(20)
print(BasePage.Base.driver.page_source)

Appium 后台报错:
[HTTP] --> POST /wd/hub/session/fb1bb72b-b0e8-4f46-ac1f-5286317cbfb4/context
[HTTP] {"name":"WEBVIEW_com.tencent.mm:appbrand0"}
[debug] [W3C (fb1bb72b)] Calling AppiumDriver.setContext() with args: ["WEBVIEW_com.tencent.mm:appbrand0","fb1bb72b-b0e8-4f46-ac1f-5286317cbfb4"]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Running 'F:\Android\android-sdk\platform-tools\adb.exe -P 5037 -s a41bc84b9805 shell cat /proc/net/unix'
[AndroidDriver] Not checking whether webviews have active pages; use the 'ensureWebviewsHavePages' cap to turn this check on
[debug] [AndroidDriver] WEBVIEW_25091 mapped to pid 25091
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Running 'F:\Android\android-sdk\platform-tools\adb.exe -P 5037 -s a41bc84b9805 shell ps'
[debug] [AndroidDriver] Returning process name: 'unknown'
[debug] [AndroidDriver] Found webviews: ["WEBVIEW_unknown"]
[debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_unknown"]
[debug] [W3C (fb1bb72b)] Encountered internal error running command: NoSuchContextError: No such context found.
[debug] [W3C (fb1bb72b)] at AndroidUiautomator2Driver.setContext (C:\Users\Cruise\AppData\Roaming\npm\node_modules\appium\node_modules\appium-android-driver\lib\commands\context.js:57:11)
[debug] [W3C (fb1bb72b)] at
[HTTP] <-- POST /wd/hub/session/fb1bb72b-b0e8-4f46-ac1f-5286317cbfb4/context 400 602 ms - 328


↙↙↙阅读原文可查看相关链接,并与作者交流