『與善仁』Appium基礎 — 22、獲取元素信息的操作


1、獲取元素文本內容

(1)text()方法

業務場景:

  1. 進入設置。
  2. 獲取所有元素class屬性為android.widget.TextView的文本內容。

代碼實現:

# 定位元素
text_vlaue = driver.find_elements_by_class_name("android.widget.TextView")

# 打印頁面中class_name為android.widget.TextView元素的文本內容
for i in text_vlaue:
    print(i.text)

(2)get_attribute()方法

# value:元素的屬性
方法: get_attribute(value) 

說明:

  • value='name'返回content-desc/text屬性值。
    content-desc/text屬性值好像是不共存的,一個元素中這兩個屬性只有一個有值。)
  • value='text'返回text的屬性值。
  • value='className'返回class屬性值,
    只有API=>18才能支持(4.2.1版本以上就可以,7.1.1 API版本是25)
  • value='resourceId'返回resource-id屬性值,
    只有API=>18才能支持持(同上)

(3)綜合練習

"""
1.學習目標
    掌握appium元素文本信息獲取
2.操作步驟
    2.1 元素.text   獲取元素text文本值(重點)
    2.2 元素.get_attribute(value)  根據value值獲取對應的內容
        value = "name"   獲取元素content-desc 或 text值(常用,重點)
        value = "text"   獲取元素text屬性值
        value = "className"  獲取元素class屬性值,Android 4.3以上版本
        value = "resourceId" 獲取元素id屬性值,Android 4.3以上版本
3.需求
    在設置APP中實現上述命令
"""
# 1.導入appium
import time
from appium import webdriver

# 2.創建Desired capabilities對象,添加啟動參數
desired_caps = {
    "platformName": "Android",  # 系統名稱
    "platformVersion": "7.1.2",  # 系統版本
    "deviceName": "127.0.0.1:21503",  # 設備名稱
    "appPackage": "com.android.settings",  # APP包名
    "appActivity": ".Settings"  # APP啟動名
}

# 3.啟動APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# 4.定位元素
# # 4.1 定位元素,搜索按鈕,藍牙
search = driver.find_element_by_id("com.android.settings:id/search")
blue_tooth = driver.find_element_by_android_uiautomator('new UiSelector().text("藍牙")')

# 5.獲取元素屬性值
# 5.1 獲取藍牙的text值
print("藍牙text屬性值: ", blue_tooth.text)
print("藍牙text屬性值: ", blue_tooth.get_attribute("text"))
# 5.2 獲取搜索的content-desc值
print("搜索的content-desc屬性值: ", search.get_attribute("name"))
# 5.3 獲取搜索的id屬性值
print("搜索的id屬性值: ", search.get_attribute("resourceId"))
# 5.4 獲取搜索的class屬性值
print("搜索的class屬性值: ", search.get_attribute("className"))

# 6.關閉APP
time.sleep(3)
driver.quit()

執行結果:

藍牙text屬性值:  藍牙
搜索的content-desc屬性值:  搜索設置
搜索的id屬性值:  com.android.settings:id/search
搜索的class屬性值:  android.widget.TextView

2、獲取元素在屏幕上的坐標

在移動端進行元素定位的時候,可能出現該元素位置不好定位,或者不能用上邊屬性的方式進行准確的定位,我們就可以用坐標的方式操作手機,如滑動操作有時候就需要用到。

使用方法:location方法。

業務場景:

  1. 進入設置頁面。
  2. 獲取搜索按鈕在屏幕的坐標位置。

代碼實現:

# 定位到搜索按鈕
get_value = driver.find_element_by_id("com.android.settings:id/search")

# 打印搜索按鈕在屏幕上的坐標
print(get_value.location)

練習:

"""
1.學習目標
    掌握appium獲取元素坐標
2.操作步驟
    元素.location   獲取元素坐標
    app頁面坐標分部:
        坐標原點-屏幕左上角(0,0)
        從左向右 x坐標,逐漸增大
        從上向下 Y坐標,逐漸增大
3.需求
    在設置APP中實現藍牙定位
"""
# 1.導入appium
import time
from appium import webdriver

# 2.創建Desired capabilities對象,添加啟動參數
desired_caps = {
    "platformName": "Android",  # 系統名稱
    "platformVersion": "7.1.2",  # 系統版本
    "deviceName": "127.0.0.1:21503",  # 設備名稱
    "appPackage": "com.android.settings",  # APP包名
    "appActivity": ".Settings"  # APP啟動名
}

# 3.啟動APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# 4.定位元素
blue_tooth = driver.find_element_by_android_uiautomator('new UiSelector().text("藍牙")')
# 4.1 獲取元素坐標
print("藍牙坐標: ", blue_tooth.location)
# 輸出結果:
# 藍牙坐標:  {'x': 86, 'y': 265}
# # 得到的坐標為元素左上角的坐標。


# 4.3 獲取手機的寬度和高度
size = driver.get_window_size()  # 獲取手機屏幕大小
print(size)  # {'width': 576, 'height': 1024}

# 6.關閉APP
time.sleep(3)
driver.quit()

提示:

我們可以獲取元素的坐標,也可以定位在屏幕中某個坐標點進行操作。


免責聲明!

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



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