• Appium+python自动化28-获取元素属性get_attribute


    获取text

    # coding:utf-8
    from appium import webdriver
    from time import sleep
    
    desired_caps = {
                    'platformName': 'Android',
                    'deviceName': '127.0.0.1:62001',
                    'platformVersion': '4.4.2',
                    'appPackage': 'com.baidu.yuedu',
                    'appActivity': 'com.baidu.yuedu.splash.SplashActivity',
                    'noReset': 'true',
                    'resetKeyboard': 'true',
                    'unicodeKeyboard': 'true'
                    }
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    
    # 等主页面activity出现
    driver.wait_activity(".base.ui.MainActivity", 10)
    
    # 点取消升级
    driver.find_element_by_id("com.baidu.yuedu:id/negativeUpgrade").click()
    
    # 获取text
    t1 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").text
    print(t1)

    tag_name

    1.tag_name实质上是获取class属性

    # 获取tag_name
    t2 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").tag_name
    print(t2)
    

    2.打印结果:android.widget.TextView

    get_attribute

    1.获取content-desc属性,这里注意了,如果content-desc属性为空,那么获取的就是text属性,不为空获取的才是content-desc属性

    2.content-desc属性为空,打印结果:书架

    # content-desc为空,获取的是text
    t3 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("name")
    print(t3)

    3.content-desc属性不为空,打印结果:百度阅读

    # content-desc
    t4 = driver.find_element_by_id("com.baidu.yuedu:id/fragment_banner").get_attribute("name")
    print t4
    

    4.id,calss,text属性获取

    # id
    t5 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("resourceId")
    print(t5)
    
    # class
    t6 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("className")
    print(t6)
    
    # text
    t7 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("text")
    print(t7)

    5.其它属性获取,注意这里并不是所有的都可以获取,一些标准的属性是可以获取到的

    # checkable
    t8 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("checkable")
    print t8
    
    # clickable
    t9 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("clickable")
    print t9
    

    size和location

    1.获取size,返回的是字典,如:{'width': 84, 'height': 84}

    2.获取location,返回的是字典,如:{'y': 38, 'x': 192}

    # size
    t10 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").size
    print t10
    
    # location
    t11 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").location
    print t11
    

    参考代码

    # coding:utf-8
    from appium import webdriver
    
    desired_caps = {
                    'platformName': 'Android',
                    'deviceName': '127.0.0.1:62001',
                    'platformVersion': '4.4.2',
                    'appPackage': 'com.baidu.yuedu',
                    'appActivity': 'com.baidu.yuedu.splash.SplashActivity',
                    'noReset': 'true',
                    'resetKeyboard': 'true',
                    'unicodeKeyboard': 'true'
                    }
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    
    # 等主页面activity出现
    driver.wait_activity(".base.ui.MainActivity", 10)
    
    # 点取消升级
    driver.find_element_by_id("com.baidu.yuedu:id/negativeUpgrade").click()
    
    # 获取text
    t1 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").text
    print(t1)
    
    # 获取tag_name
    t2 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").tag_name
    print(t2)
    
    # content-desc为空,获取的是text
    t3 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("name")
    print(t3)
    
    # content-desc
    t4 = driver.find_element_by_id("com.baidu.yuedu:id/fragment_banner").get_attribute("name")
    print t4
    
    # id
    t5 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("resourceId")
    print(t5)
    
    # class
    t6 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("className")
    print(t6)
    
    # text
    t7 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("text")
    print(t7)
    
    # checkable
    t8 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("checkable")
    print t8
    
    # clickable
    t9 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").get_attribute("clickable")
    print t9
    
    # size
    t10 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").size
    print t10
    
    # location
    t11 = driver.find_element_by_id("com.baidu.yuedu:id/lefttitle").location
    print t11

  • 相关阅读:
    GridControl主从表设置
    Asp.net Ajax框架教程
    实现类似百度下拉框自动匹配功能
    将一个DataTable分解成多个DataTable
    找不到可安装的ISAM ,asp.net读取数据丢失,解决的一列里有字符与数字的
    StringCollection FAQ [C#, BCL]
    从枚举的初始化说起 [C#]
    当多态遇上数组 ... [C++] (Rewritten)
    我并不是不闻不问![C#]
    当多态遇上数组 ... [C++, C++/CLI, C#]
  • 原文地址:https://www.cnblogs.com/zz-yy/p/8432825.html
Copyright © 2020-2023  润新知