之前整理:https://www.cnblogs.com/xiaobaibailongma/category/1781594.html
混合应用H5:
微信小程序:
"""
============================
Author:柠檬班-木森
Time:2021/3/11 20:05
E-mail:[email protected]
Company:湖南零檬信息技术有限公司
=======
"""
import time
"""
获取进程号命令:adb shell dumpsys activity top |findstr ACTIVITY
通过进程号获取进程名称: adb shell ps 进程号
com.tencent.mm:appbrand0
"""
from appium.webdriver import Remote
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
desired_cap = {
"platformName": "Android", # 手机系统
"platformVersion": "9", # 系统版本
"deviceName": 'HUAWEI', # 手机的名称
"appPackage": "com.tencent.mm", # app的包名
"appActivity": "com.tencent.mm.ui.LauncherUI", # app的启动页面
"noReset": True,
# 指定chromedriver驱动
"chromedriverExecutable": "C:\\ChromeDriver\\77\\chromedriver.exe",
"recreateChromeDriverSessions": True,
"unicodeKeyboard": True,
# 指定小程序的进程名
"chromeOptions": {"androidProcess": "com.tencent.mm:appbrand0"}
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities=desired_cap)
driver.implicitly_wait(15)
def click_application(driver, name):
"""
点击进入小程序"
:param name: 小程序的名字
:return:
"""""
# 等待微信启动,发现这个菜单处于可见状态,
# 点击发现
driver.find_element_by_android_uiautomator('new UiSelector().text("发现")').click()
# 点击小程序
driver.find_element_by_android_uiautomator('new UiSelector().text("小程序")').click()
# 点击柠檬班软件测试
driver.find_element_by_android_uiautomator('new UiSelector().text("{}")'.format(name)).click()
# # ============================进入小程序页面================================
# # 获取context
# cos = driver.contexts
# print(cos)
def switch_to_contenxt(driver, loc):
"""切换到webview中的对应窗口"""
time.sleep(10)
# 切换上下文,进入webview中
driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0')
# 获取WebView中所有的窗口
wins = driver.window_handles
for win in wins:
driver.switch_to.window(win)
try:
driver.find_element(*loc)
except:
pass
else:
break
# click_application(driver, '柠檬班软件测试')
#
# switch_to_contenxt(driver, (MobileBy.XPATH, "//a[contains(text(),'课程')]"))
click_application(driver, '软件测试柠檬班')
#
switch_to_contenxt(driver, (MobileBy.XPATH, "//*[contains(text(),'点击绑定柠檬班账号')]"))
# ==================进入h5页面中==============
driver.find_element_by_xpath("//*[contains(text(),'点击绑定柠檬班账号')]").click()