3、app自动化:使用appium定位元素的方式及元素的常用操作

前提

没有的包,要先进行对应包的安装

如:pip install Appium-Python-Client

一、定位元素,包括属性定位xpath定位方式

a\属性定位

属性 定位方式     示例
content-desc AccessibilityID driver.find_element(By.AccessibilityID,'书城')
Class name& Tag name Class name  
resource-id(安卓) ID  
name(IOS) ID  

 

 

复制代码
import time
from appium.webdriver import Remote
from appium.webdriver.common.mobileby import MobileBy as By

rl = "http://127.0.0.1:4723/wd/hub"
caps = {
    "platformName": "Android",
    "platformVersion": "10.0.0",
    "deviceName": "xxxxx",
    "appPackage": "comxxxx",
    "appActivity": "com.zxxx.MainActivity",
    "newCommandTimeout": 10,
    # "noReset": True  # 注释掉,是因为我需要每次进入的时候,没有任何书籍在书架中
}
driver = Remote(url, caps)

ele = driver.find_element(By.ACCESSIBILITY_ID, "书城")
复制代码

b\路径定位

  根据元素之间的层级关系(或者说路径)进行定位,是appium里目前适用性最高的定位方式

 

如:

driver.find_element(By.XPATH,'//android.widget.FrameLayout[@resourceid="android:id/content"]')

c\拓展 之 元素内定位

  当元素内还包含一个或多个其他元素内,就可以先定位到该元素,然后再通过该元素去定位其下面的元素,这种方式就叫元素内定位。

下面用一段伪代码来说明:

ele=driver.find_elements(By.XPATH,xpath)
ele[1].find_element(By.ID,'xxxx')

 

 

二、元素的操作

1、元素的点击

ele.click()

2、输入

ele.send_keys('输入的值')

3、获取文本

ele.text

4、元素截图

ele.screenshot('name.jpg')

5、获取元素属性

ele.get_attribute("text")
ele.get_attribute("class")
ele.get_attribute("displayed")
ele.get_attribute("content-desc")

6、其他一些比较重要的属性

ele.location #The location of the element in the renderable canvas. 
ele.size #The size of the element.

print(ele.location)  # 元素的位置属性  {'x': 606, 'y': 108}
print(ele.size)  # 元素的尺寸属性 {'height': 115, 'width': 132}

# 确定元素的操作点,一般是:元素的中心点
# 其计算公式是: (x+width/2,y+height/2)

 

三 appium的系统操作

1、返回 driver.back()

2、滑动 driver.swipe(700,1700,700,500)

3、截图 driver.save_screenshot("整体截图.png")

posted @   袁小文子  阅读(428)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示