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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM