from appium import webdriver
import time
#QQ群522720170
desired_caps = {}
desired_caps = {
'platformName': 'Android',
'platformVersion': '23',
'deviceName': 'Android Emulator',
'unicodeKeyboard': 'True',
'resetKeyboard': 'True',
'appPackage': 'com.tencent.mm',
'appActivity': 'com.tencent.mm.ui.LauncherUI',
'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10)
driver.find_element('name','发现').click()
time.sleep(10)
driver.find_element('name',"看一看").click()
time.sleep(10)
#获取当前上下文
c=driver.contexts
print(c)
#输出结果['NATIVE_APP', 'WEBVIEW_com.tencent.mm:tools']
#切换为 webview,名称就是从上面的语句得来的
driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
#获取h4标签的文本并打印出来
titles = driver.find_elements('tag name','h4')
print(titles[2].text)