Python-uiautomator2學習2--元素查看工具weditor

前言

前面我們已經使用 uiautomator2 成功連接到手機了,我們還需要一個工具來查看手機App上的元素,另外該項目作者也針對 uiautomator2 寫了一個工具 weditor ,我們可以使用該工具來查看元素。

更多的介紹,大家可以前往 Github 進行學習:https://github.com/openatx/weditor

安裝 weditor

通過 Python 的 pip 工具進行安裝,命令如下:pip install --pre weditor

(venv) D:\pycharm\Code\atxDemo2\venv\Scripts>pip install --pre weditor
Collecting weditor
  Downloading https://files.pythonhosted.org/packages/be/70/8b89a13a1a144e62f688aad68f3862c7206ff435f5fd10e9350318ab3221/weditor-0.6.4-py3-none-any.whl (1.6MB)
    100% |████████████████████████████████| 1.6MB 559kB/s
Requirement already satisfied: uiautomator2>=2.10.1 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from weditor) (2.15.2)
Requirement already satisfied: cached-property in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from weditor) (1.5.2)
Collecting facebook-wda>=1.0.5 (from weditor)
  Downloading https://files.pythonhosted.org/packages/ec/c3/1445667bdc4b3ee450c24bfbff04f26244a82a3209676759971e88663f0a/facebook_wda-1.4.0-py3-none-any.whl
Requirement already satisfied: six in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from weditor) (1.16.0)
Collecting tornado>=4.3 (from weditor)
  Downloading https://files.pythonhosted.org/packages/c7/c2/ff4628a08df5ce2662109957275718888d7ab6787591fa0decfd327bd2ce/tornado-6.1-cp37-cp37m-win_amd64.whl (422kB)
    100% |████████████████████████████████| 430kB 628kB/s
Requirement already satisfied: logzero in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from weditor) (1.7.0)
Collecting pywin32; sys_platform == "win32" (from weditor)
  Downloading https://files.pythonhosted.org/packages/25/54/177ee28fec4ecd23fa539f3df78067e7a4927515b84eac34e36060fcdb8d/pywin32-300-cp37-cp37m-win_amd64.whl (9.2MB)
    100% |████████████████████████████████| 9.2MB 1.4MB/s
Requirement already satisfied: pillow in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from weditor) (8.2.0)
Requirement already satisfied: requests in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from uiautomator2>=2.10.1->weditor) (2.25.1)
Requirement already satisfied: whichcraft in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from uiautomator2>=2.10.1->weditor) (0.6.1)
Requirement already satisfied: progress~=1.3 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from uiautomator2>=2.10.1->weditor) (1.5)
Requirement already satisfied: retry~=0.9 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from uiautomator2>=2.10.1->weditor) (0.9.2)
Requirement already satisfied: adbutils<1.0,>=0.11.0 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from uiautomator2>=2.10.1->weditor) (0.11.0)
Requirement already satisfied: Deprecated~=1.2.6 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from uiautomator2>=2.10.1->weditor) (1.2.12)
Requirement already satisfied: lxml>=4.3 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from uiautomator2>=2.10.1->weditor) (4.6.3)
Requirement already satisfied: packaging~=20.3 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from uiautomator2>=2.10.1->weditor) (20.9)
Requirement already satisfied: colorama; sys_platform == "win32" in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from logzero->weditor) (0.4.4)
Requirement already satisfied: chardet<5,>=3.0.2 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from requests->uiautomator2>=2.10.1->weditor) (4.0.0)
Requirement already satisfied: idna<3,>=2.5 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from requests->uiautomator2>=2.10.1->weditor) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from requests->uiautomator2>=2.10.1->weditor) (2020.12.5)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from requests->uiautomator2>=2.10.1->weditor) (1.26.4)
Requirement already satisfied: py<2.0.0,>=1.4.26 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from retry~=0.9->uiautomator2>=2.10.1->weditor) (1.10.0)
Requirement already satisfied: decorator>=3.4.2 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from retry~=0.9->uiautomator2>=2.10.1->weditor) (5.0.9)
Requirement already satisfied: deprecation<3.0,>=2.0.6 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor) (2.1.0)
Requirement already satisfied: apkutils2<2.0,>=1.0.0 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor) (1.0.0)
Requirement already satisfied: wrapt<2,>=1.10 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from Deprecated~=1.2.6->uiautomator2>=2.10.1->weditor) (1.12.1)
Requirement already satisfied: pyparsing>=2.0.2 in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from packaging~=20.3->uiautomator2>=2.10.1->weditor) (3.0.0b2)
Requirement already satisfied: pyelftools in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor) (0.27)
Requirement already satisfied: cigam in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor) (0.0.3)
Requirement already satisfied: xmltodict in d:\pycharm\code\atxdemo2\venv\lib\site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor) (0.12.0)
Installing collected packages: facebook-wda, tornado, pywin32, weditor
Successfully installed facebook-wda-1.4.0 pywin32-300 tornado-6.1 weditor-0.6.4

安裝完成後,使用該工具的命令如下:python -m weditor

(venv) D:\pycharm\Code\atxDemo2\venv\Scripts>python -m weditor
listening on http://192.168.1.13:17310
[I 210522 13:05:20 web:2243] 200 GET / (::1) 140.85ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/bootstrap/3.3.7/css/bootstrap.min.css (::1) 718.79ms
[I 210522 13:05:20 web:2243] 200 GET /unpkg.com/element-ui/lib/theme-chalk/index.css (::1) 4.99ms
[I 210522 13:05:20 web:2243] 200 GET /static/libs/css/buttons.css (::1) 6.98ms
[I 210522 13:05:20 web:2243] 200 GET /static/libs/fontawesome/css/font-awesome.min.css (::1) 7.98ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/bootstrap.select/1.12.2/css/bootstrap-select.min.css (::1) 9.01ms
[I 210522 13:05:20 web:2243] 200 GET /static/style.css?v=b3b1e4acb8252e52c852bf94b932b61cec1f88758b2a87f79c05ed639e8ba56480596828b9d5283ef722507bee00b6b583a2bee79cb91a2eeab4f16293c301ae (::1) 40.72ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/npm/[email protected]/dist/themes/default/style.min.css (::1) 41.69ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js (::1) 2.03ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/bootstrap/3.3.7/js/bootstrap.min.js (::1) 2.03ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/npm/[email protected]/src-noconflict/ace.min.js (::1) 5.01ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/bootstrap.select/1.12.2/js/bootstrap-select.min.js (::1) 5.98ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/npm/[email protected]/src-noconflict/mode-python.min.js (::1) 1.00ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/npm/[email protected]/src-noconflict/ext-language_tools.min.js (::1) 1.00ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/npm/[email protected]/src-noconflict/ext-searchbox.min.js (::1) 2.00ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/npm/[email protected]/src-noconflict/theme-monokai.js (::1) 0.97ms
[I 210522 13:05:20 web:2243] 200 GET /cdn.jsdelivr.net/npm/[email protected]/dist/jstree.min.js (::1) 2.99ms
[I 210522 13:05:20 web:2243] 200 GET /static/libs/vue-2.5.16/vue.js?v=1f2d3ad19a9273bbe648fe19b42206f73c6173dfe1123b96b67b2fdc2d6ca8fa3e24b59ae493ac0ded1a6645728fbbe47edf5dd43d3b3c307746b50e1778f319 (::1) 106.59ms
[I 210522 13:05:21 web:2243] 200 GET /static/js/index.js?v=bd966504b94d2b7792d21be18adb95649b35787f891ab722bb44d6f473eefe8276b31e727d895966f80f5640ac821d75e72b41356f77a9a1b6e5bf35ff90b73c (::1) 34.48ms
[I 210522 13:05:21 web:2243] 200 GET /static/js/common.js?v=a72c25bcd36a60339ae8d48ccb96531f4d23c3120bb6769e6a6763b95956233d3262ea33ff29e22c507ca74468c687bd571f1cb39fa9290078ec23c68117311a (::1) 55.29ms
[I 210522 13:05:21 web:2243] 200 GET /static/running.svg (::1) 60.28ms
[I 210522 13:05:21 web:2243] 200 GET /unpkg.com/element-ui/lib/index.js (::1) 70.26ms
[I 210522 13:05:21 web:2243] 200 GET /static/loading.svg (::1) 73.24ms
[I 210522 13:05:22 web:2243] 200 GET /static/libs/fontawesome/fonts/fontawesome-webfont.woff2?v=4.7.0 (::1) 2.99ms
[I 210522 13:05:22 web:2243] 200 GET /cdn.jsdelivr.net/bootstrap/3.3.7/fonts/glyphicons-halflings-regular.woff2 (::1) 1.00ms
[I 210522 13:05:23 web:2243] 200 GET /api/v1/version (::1) 1.00ms
[I 210522 13:05:23 shell:47] Started drain subprocess stdout in thread
[I 210522 13:05:23 web:2243] 101 GET /ws/v1/python (::1) 125.17ms
[I 210522 13:05:23 shell:189] create process pid: 13548
[I 210522 13:05:23 web:2243] 200 GET /unpkg.com/element-ui/lib/theme-chalk/fonts/element-icons.woff (::1) 75.31ms
[I 210522 13:05:24 web:2243] 200 GET /favicon.ico (::1) 1.99ms
* daemon not running; starting now at tcp:5037
* daemon started successfully

啓動成功後,瀏覽器會默認打開一個Web網頁:http://localhost:17310 ,之後我們將在該頁面查看元素。

需要注意的是,我們在使用過程中,不要把命令行窗口關閉,否則將不能在上面Web頁面中查看元素。

爲了更方便使用,我們也可以創建啓動 weditor 的快捷方式圖標,執行命令:python -m weditor --shortcut

(venv) D:\pycharm\Code\atxDemo2\venv\Scripts>python -m weditor --shortcut
Shortcut created. C:\Users\wintest\Desktop\WEditor.lnk

weditor快捷方式

weditor 連接手機

首先,在命令行窗口輸入命令:adb devices 查看連接狀態,確保手機已經連接上電腦。

D:\>adb devices
List of devices attached
c01bcd5d        device

接着在Web頁面上,點擊 Connect 按鈕,當看到出現一個綠色的小葉子,就說明連接成功。

Web頁面

頁面元素定位

計算器

接下來,我們根據上面的計算器來對頁面元素的定位進一步說明和實踐。

  • ResourceId 定位
# 點擊鍵盤的數字 1
d(resourceId="com.sec.android.app.popupcalculator:id/bt_01").click()
# 點擊鍵盤的加號 +
d(resourceId="com.sec.android.app.popupcalculator:id/bt_add").click()
  • Text 定位
# 點擊鍵盤的數字 5
d(text="5").click()
# 點擊鍵盤的等號 =
d(text="=").click()
  • Description 定位
# 點擊鍵盤的加號 +
d(description="加號").click()
# 點擊鍵盤的等號 =
d(description="等於").click()
  • ClassName 定位
# 點擊計算器最上方的文本區域
d(className="android.widget.EditText").click()
  • XPath 定位
# 點擊鍵盤的數字 1,對應 ResourceId 屬性
d.xpath('//*[@resource-id="com.sec.android.app.popupcalculator:id/bt_01"]').click()
# 點擊鍵盤的加號 1,對應 Text 屬性
d.xpath('//*[@text="1"]').click()
# 點擊鍵盤的等號 =,對應 Description 屬性
d.xpath('//*[@content-desc="等於"]').click()
  • 座標 定位
# 點擊鍵盤的數字 1,座標大致爲 (0.1, 0.8)
d.click(0.1, 0.8)
# 點擊鍵盤的等號 =,座標大致爲 (0.8, 0.9)
d.click(0.8, 0.9)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章