dylancoding

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

做App UI自动化的时候,appium使用最频繁的应该就是定位元素了。

Appium常用的元素属性值:

resource-id:resource-id属性一般ID是唯一的,如果元素中有ID的话,优先考虑ID定位: 

定位方法:

driver.find_element_by_id('resource-id属性值')

class_name:通过元素class属性值来定位元素:

定位方法:

driver.find_element_class_name('class_name属性值') 

xpath:通过元素的xpath来进行定位:

定位方法:

driver.find_element_by_xpath("//*[@text='value']")

if 定位不到元素会出现NoSuchElementException,我们可以进行组合定位:

driver.find_element_by_xpath("//*[@text ='value' and @class = 'classvalue']”)

其中:

//    # 全局范围
*     # 增加一个筛选的标签条件
[]    # 添加筛选的条件
@     #表示添加的条件是属性
text  #通过文本来查找
value #属性的值
and   # 可以进行多个条件的关联

 通过以上的3个方法可以获取元素的进行,下面就可以对元素进行对应的操作。

Appium元素的操作:

driver.find_element_by_id('resource-id属性值').click

  # 对应resource-id进行点击

driver.find_element_by_id('resource-id').clear

  # 对应的resource-id进行清除

driver.find_element_by_class_name('android.widget.EditText').send_keys('value')

  # 对应的class_name进行输入value

Appium获取元素的信息:

driver.find_element_by_id('resource-id').text

  # 获取resource-id的文本

driver.find_element_by_id('resource-id').location

  # 获取resource-id的位置,返回字典,x,y是元素左上角的坐标

driver.find_element_by_id('resource-id').size

  # 获取resource-id元素的大小,返回字典,包含width,height

driver.get_attribute(attribute)

  # attribute:可以获取resource-id,class,text,package,name

 

posted on 2022-09-05 14:57  dylan7  阅读(291)  评论(0)    收藏  举报