python+appium微信小程序自动化实现

一、安装Appium

 

二、Appium SDK配置

 

 三、chromedriver驱动路径配置及appium启动

  1.查看X5内核版本

  微信小程序是基于goole的webview 做了封装了,叫x5内核,所以跟chrome浏览器定位元素一样,需要配置chromedriver.exe,

  通过 Uc-devtools 工具可以识别到 Chrome是什么版本,再去下载对应的chromedriver.exe

 

 

  2.启动Appium前配置驱动路径,否则运行报错

 

 四、微信打开调试模式

  给任意一个聊天窗口发送链接:debugx5.qq.com

五、指定Android进程

  通过adb命令查看当前进程(关闭手机多余后台进程,只留被测小程序/公众号,免受影响)

  放在实例化Remote配置信息中

  "chromeOptions": {"androidProcess": "com.tencent.mm:appbrand0"}

 五、源码

 1 from appium import webdriver
 2 from selenium.webdriver.support.wait import WebDriverWait
 3 from selenium.webdriver.support import expected_conditions as EC
 4 from selenium.webdriver.common.by import By
 5 from appium.webdriver.common.touch_action import TouchAction
 6 from time import sleep
 7 
 8 from common.SwipeOp import swipe_op
 9 
10 
11 class xiaochengxu():
12     def __init__(self):
13         desired_caps = {
14             "platformName": "Android",  # 使用的手机操作系统
15             "platformVersion": "9",  # 系统版本
16             "udid": "6EBDU17320000355",
17             "appPackage": "com.tencent.mm",  # 应用包名
18             "appActivity": "ui.LauncherUI",  # Activity
19             "noReset": True,  # 启动app时不初始化
20             "automationName": "uiautomator2",
21             "chromeOptions": {"androidProcess": "com.tencent.mm:appbrand0"}
22         }
23 
24 
25         # 与appium server进行连接,并发送要操作的设备信息
26         self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
27         sleep(10)
28 
29     def operate(self):
30         # 向下滑动
31         swipe_op(self.driver).swipe_down()        
32         try:
33             DCD = '//*[@text="懂车帝App"]'
34             WebDriverWait(self.driver, 100).until(EC.visibility_of_element_located((By.XPATH, DCD)))
35             self.driver.find_element_by_xpath(DCD).click()
36             sleep(3)
37         except:
38             pass
39         # 获取当前content
40         contexts = self.driver.contexts
41         print(contexts)
42         # 切换上下文进入小程序
43         self.driver.switch_to.context(contexts[-1])
44         print("当前所在的上下文: ", self.driver.current_context)
45         # 获取所有窗口句柄
46         all_handles = self.driver.window_handles
47         print(all_handles)
48         # 当前会有很多窗口句柄,需要遍历获取元素,切换到当前的windowHandle
49         for handle in all_handles:
50             try:
51                 self.driver.switch_to.window(handle)
52                 self.driver.find_element_by_xpath('//*[@id="_n_20"]').click()
53                 # self.driver.find_element_by_accessibility_id("_n_73").click()
54                 print('定位成功')
55             except Exception:
56                 print("定位失败")
57 
58 
59 if __name__ == '__main__':
60     xiaochengxu().operate()

 

 

遇到的问题:

1.不强制Android进程报错如下:

selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: An unknown server-side error occurred while processing the command. Original error: unknown error: Failed to get sockets matching: @webview_devtools_remote_.*25279
(make sure the app has its WebView configured for debugging)

 

posted @ 2021-03-05 15:44  Wiselee😏  阅读(2513)  评论(1编辑  收藏  举报