appium+python 获取元素属性

般标准的属性我们都可以通过get_attribute(“属性名称”)来获取。

常见的属性如下:

先通过xpath方式定位到这个元素

ele = self.driver.find_element_by_xpath("//android.widget.EditText[@resource-id='co***s:id/et_cm3Main_search']")

 获取 text值:


     text_str1 = ele.text  # 法一。一般都用这种,比较简单
     text_str1 = ele.get_attribute("text")  # 法二

获取resource-id值:

id_str = ele.get_attribute("resource-id")

获取classname值:(两种方法)

classname_str1 = ele.get_attribute("className")  # 法一
classname_str2 = ele.tag_name  # 法二

获取content-desc值:

content_desc_str = ele.get_attribute("name")  # 获取content-desc的值,如果为空,则返回text的值

获取checked值:

checked_bool1 = ele.get_attribute("checked")  # 法一
checked_bool2 = ele.is_checked()  # 法二
# 获取元素是否是checked的对象,是返回true,不是返回false

元素里只要属性值是布尔型的,即false or true。都可以通过这个方法来获取,就不一一列出了。(clickable enabled等等)

这里的第二种方法经常用到,重点掌握!!

原文地址:https://www.cnblogs.com/may18/p/14510338.html