from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy as MB
from appium import webdriver
import time
desired_caps = {}
# 支持X5内核应用自动化配置
desired_caps["recreateChromeDriverSessions"] = True
# android 4.4以下的版本通过Selendroid来切换到webview
desired_caps["automationName"] = "UiAutomator2"
desired_caps["platformName"] = "Android"
desired_caps["deviceName"] = "Android Emulator"
desired_caps["appPackage"] = "com.tencent.mm"
desired_caps["appActivity"] = "com.tencent.mm.ui.LauncherUI"
# 内置浏览器driver所在位置
desired_caps["chromedriverExecutableDir"] = r'D:\data\chromedriver'
desired_caps["noReset"] = True
desired_caps["chromeOptions"] = {"androidProcess": "com.tencent.mm:appbrand0"}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(10)
# 进入微信主页的点击聊天
el = driver.find_element('id', 'com.tencent.mm:id/baj')
size = driver.get_window_size()
el.click()
# 顶部向下滑动,进去我的小程序
driver.swipe(size["width"] * 0.5, size["height"] * 0.1, size["width"] * 0.5, size["height"] * 0.9, 100)
# 下拉列表当中,点击 小程序
el = driver.find_element('xpath', '//*[contains(@text, "小程序")]')
# el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located(locator))
el.click()
time.sleep(3)
# ================== 进入了 指定小程序内部界面===============
# 获取所有的上下文
cons = driver.contexts
print("当前所有的上下文为:", cons)
# 切换到小程序webview
#driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0')
driver.switch_to.context[-1] #进入小程序后与H5混合应用测试一致。
time.sleep(3) driver.quit()