1、獲取元素文本內容
(1)text()方法
業務場景:
- 進⼊設置
- 獲取所有元素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
方法
業務場景:
- 進⼊設置頁⾯
- 獲取搜索按鈕在屏幕的座標位置
代碼實現:
# 定位到搜索按鈕
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()
提示:
我們可以獲取元素的座標,也可以定位在屏幕中某個座標點進行操作。