感谢@qiqiqinqin 同学
如何切换 webview 进入小程序请参考https://testerhome.com/topics/12003

脚本思路:
进入 webview 后会存在多个 handle 同 Web 页签一样,获取所有的 handle,然后在遍历所有的 handle,通过 switch_to_window 进行 handle 切换,当某个 handle 可以定位到我们需要定位的元素时,然后我们就可以进行之后的自动化操作了!

#coding=utf-8
from appium import webdriver
import time,os,re
from appium.webdriver.common.touch_action import TouchAction


desired_caps = {}
desired_caps['platformName'] = 'Android'    
desired_caps['deviceName'] = 'Android001'   
desired_caps['unicodeKeyboard'] = True      
desired_caps["resetKeyboard"] = True        
desired_caps["newCommandTimeout"]=30       
desired_caps['fullReset'] = 'false'     
desired_caps['appPackage'] = 'com.tencent.mm'           
desired_caps['appActivity'] = 'com.tencent.mm.ui.LauncherUI'        
desired_caps['recreateChromeDriverSessions'] = True
desired_caps['noReset'] = True
desired_caps['newCommandTimeout'] = 10
desired_caps['chromeOptions']={'androidProcess': 'com.tencent.mm:appbrand0'}
driver = webdriver.Remote(command_executor = 'http://127.0.0.1:4723/wd/hub',desired_capabilities = desired_caps)  
time.sleep(2)
time.sleep(1)
driver.implicitly_wait(10)
driver.find_element_by_name('发现').click()
time.sleep(1)
driver.swipe(100,1200,100,900)
driver.find_element_by_name('小程序').click()
driver.find_element_by_name('美团外卖').click()
time.sleep(2)
contexts = driver.contexts
print contexts
time.sleep(2)
driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0')
print '切换成功'
print driver.current_context
all_handles = driver.window_handles
print len(all_handles)
for handle in all_handles:
    try:
        driver.switch_to_window(handle)
        print driver.page_source
        driver.find_element_by_css_selector('.filter-select.flex-center')    #定位“筛选 ”按钮
        print '定位成功'
        break
    except Exception as e:
        print e
driver.find_element_by_css_selector('.filter-select.flex-center').click()
time.sleep(5)
driver.quit()


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