Appium+python自動化4-元素定位uiautomatorviewer

前言   python

環境搭建好了,下一步元素定位,元素定位本篇主要介紹如何使用uiautomatorviewer,經過定位到頁面上的元素,而後進行相應的點擊等操做.

uiautomatorviewer是android-sdk自帶的一個元素定位工具,很是簡單好用,使用uiautomatorviewer,你能夠檢查一個應用的UI來查看應用的佈局和組件以及相關的屬性。android

1、啓動uiautomatorviewer.batios

    1.打開目錄D:\androidsdk\android-sdk-windows\toolsweb

    2.雙擊啓動,啓動以後出現以下界面windows

 

    3若是不喜歡雙擊啓動的話,也能夠在cmd裏面經過指令啓動app

先cd到tools目錄下,而後輸入uiautomatorviewer.bat回車後啓動服務工具



2、鏈接手機佈局

    1.cmd打開輸入adb devices,確認手機已連上學習

    2.打開手機淘寶頁面,讓屏幕處於點亮狀態ui

    3.點左上角安卓機器人按鈕Devices Screenshot按鈕刷新頁面

    1.移動鼠標到須要定位的元素上,如搜索輸入框

   2.右下角能夠看到元素對應的屬性

text:搭配新寵不能缺

resource-id:com.taobao.taobao:id/home_searchedit

class:android.widget.EditText



4、點搜索框

    1.前面一篇啓動app後,休眠五秒,等待頁面加載完成

    2.經過id來定位到搜索框,而後點擊

5、最終代碼以下

# coding=utf-8

from appium import webdriver

import time

desired_caps = {

                'platformName': 'Android',

                'deviceName': '30d4e606',

                'platformVersion': '5.0',

                'appPackage': 'com.taobao.taobao',

                'appActivity': 'com.taobao.tao.welcome.Welcome',

                }

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

# 休眠五秒等待頁面加載完成

time.sleep(5)

driver.find_element_by_id("com.taobao.taobao:id/home_searchedit").click()


6、元素定位

(此處敲黑板,作筆記!!!)

    1.appium的webdriver提供了11種元素定位方法,在selenium的基礎上擴展了三個,能夠在pycharm裏面輸入driver.find_element_by而後會自動匹配出來

   2.多的三種以下,在後面的會詳細介紹

driver.find_element_by_accessibility_id()

driver.find_element_by_android_uiautomator()

driver.find_element_by_ios_uiautomation()

(第三個是ios的能夠暫時不用管)


uiautomatorviewer是android sdk自帶的,下篇介紹appium自帶個元素定位工具:Inspector.在

在學習過程當中有遇到疑問的,能夠加appium+python QQ羣交流:330467341

相關文章
相關標籤/搜索