首页 > 其他分享 >3、app自动化:使用appium定位元素的方式及元素的常用操作

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

时间:2023-01-18 10:00:46浏览次数:44  
标签:定位 appium app 元素 driver ele element

前提

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

如: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")

标签:定位,appium,app,元素,driver,ele,element
From: https://www.cnblogs.com/yxm-yxwz/p/17024292.html

相关文章