1:查看当前微信正在运行的微信公众号进程
C:\Users\edz>adb shell dumpsys activity top | findstr ACTIVITY
ACTIVITY com.netease.cloudmusic/.activity.LockScreenActivity cb44411 pid=3869
ACTIVITY com.tencent.mobileqq/.activity.SplashActivity 21ccf14 pid=6919
ACTIVITY com.tencent.mm/.plugin.webview.ui.tools.WebViewUI 9e57c31 pid=18417
ACTIVITY net.oneplus.launcher/net.oneplus.quickstep.RecentsActivity 9818f4e pid=3098
ACTIVITY net.oneplus.h2launcher/.Launcher 71ce3d9 pid=3116
ACTIVITY com.oneplus.gallery/.OPGalleryActivity 4b4055 pid=12000
2:查看当前进程的名称
C:\Users\edz>adb shell ps 18417
USER PID PPID VSZ RSS WCHAN ADDR S NAME
u0_a96 18417 884 2323872 217064 SyS_epoll_wait 0 S com.tencent.mm:tools
3:操作微信公众号或者小程序的desired_caps有:
desired_caps={ 'platformName':'Android', 'uiautomationName':'uiautomator2', 'platformVersion':'9', 'deviceName':'e8dfc3c6', 'appPackage':'com.tencent.mm', 'appActivity':'.ui.LauncherUI', 'automationName':'com.taobao.tao.welcome.Welcome', 'unicodeKeyboard':True, 'resetKeyboard':True, 'noReset':True, 'chromedriverExecutable':'D:\Program Files\chromedriver\\2.37\\chromedriver.exe',#指定微信小程序运行的chrome路径 'chromeOptions':{'androidProcess':'com.tencent.mm:tools'},#指定微信小程序运行的进程 'browserName':"", "recreateChromeDriverSessions":True }
4:在进入H5页面后,需要切换上下文
print(driver.contexts)#打印所有的上下文 print(driver.current_context)#获取当前上下文 driver.switch_to.context("WEBVIEW_com.tencent.mm:tools")#跳转到我们需要的上下文 print(driver.current_context) 运行结果: ['NATIVE_APP', 'WEBVIEW_com.tencent.mm:tools'] NATIVE_APP WEBVIEW_com.tencent.mm:tools
5:有的微信小程序在切换上下文后还是不能操作,可能就要跳转handler,因为不确定当前处于哪个窗口,所以可以这样做
handles=driver.window_handles for handle in handles: driver.switch_to.window(handle) if driver.page_source.find("患者姓名")!=-1: break